Types, Values, Properties - TypeSpec's types #3128
-
I opened an issue for an ICE and my intuition for how TypeSpec's types work wasn't correct. I went hunting in the docs, but I still don't quite have a working mental model for how types and properties in TypeSpec interact. This post is sadly a bit of a stream of consciousness, I am not exactly sure what I don't know. From my initial understanding of the world, the type of @pattern("[a-z]+")
foo: string;
There also seems to be a distinction between types (a fixed inbuilt number + user model A {
@pattern("[a-z]+")
foo: string;
}
model B {
foo: A.foo;
}
model C extends A {
foo: string
} but this one does not: model A {
@pattern("[a-z]+")
foo: string;
}
model B {
foo: A.foo;
}
model C extends B {
foo: string
} while this one does again: model A {
@pattern("[a-z]+")
foo: string;
}
model B {
foo: A.foo::type;
}
model C extends B {
foo: string
} What is the "type model A {
@pattern("[a-z]+")
foo: string;
}
model B {
foo: A.foo;
}
model C extends B {
@pattern("[a-z]+")
foo: string
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
First lets separate the bugs:model B {
foo: A.foo::type;
} This is broken today, however what it would mean is that the property type of Now for referencing properties:When you reference a property it is its own type(that property) this means even though the property type is a string, you cannot assign a string to i(why 2nd and last case fails as overrides need to be assignable to the parent). The issue is that the property can contain additional information which could make it incorrect to assume it is interchangeable with its type. |
Beta Was this translation helpful? Give feedback.
First lets separate the bugs:
This is broken today, however what it would mean is that the property type of
B.foo
is exactly astring
(would be the equivalent of doingA["foo"]
in typescriptNow for referencing properties:
When you reference a property it is its own type(that property) this means even though the property type is a string, you cannot assign a string to i(why 2nd and last case fails as overrides need to be assignable to the parent). The issue is that the property can contain additional information which could make it incorrect to assume it is interchangeable with its type.