typespec icon indicating copy to clipboard operation
typespec copied to clipboard

Reusable prop declaration

Open timotheeguerin opened this issue 1 year ago • 2 comments

Clear and concise description of the problem

We have been using model with a single prop as a way to reuse a common prop in our libraries quite a bit. This works ok but has the issue of not being able to attach extra decorators to that property.

@pattern("[a-z]+")
prop resourceName: string;


model MyResource {
  /** MY resource name doc */
 @minLength(4)
  resourceName;
}

Probably want those to be template type

@pattern("[a-z]+")
@foo(T)
prop resourceName<T>: string;

Checklist

  • [X] Follow our Code of Conduct
  • [X] Read the docs.
  • [X] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

timotheeguerin avatar Jun 06 '24 17:06 timotheeguerin

What are you thinking for optional properties? prop foo?<T>: T?

Also I wonder if we should consider this as part of Optional<T> where { x?: string } and x: Optional<string> are equivalent?

bterlson avatar Jun 06 '24 17:06 bterlson

I think it make sense to allow it to be optional but maybe that should be via that Optional<string> syntax only.

Its also maybe something that should be allowed to be overriden

@pattern("[a-z]+")
prop resourceName: string;

model MyResource {
  name?: resourceName;
}

but that at this point is no different than referencing a model property from a model

timotheeguerin avatar Jun 06 '24 18:06 timotheeguerin