You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
interfaceOrig<Textendsstring>{type: T;}typeTypeId='SpecA';interfaceSpecAextendsOrig<TypeId>{name?: string;}constgoodCast=<SpecA>{type: 'SpecA'}constbadCast=<SpecA>{type: 'SpecB'}// throws an error:// Type '{ type: "SpecB"; }' cannot be converted to type 'SpecA'.// Types of property 'type' are incompatible.// Type '"SpecB"' is not comparable to type '"SpecA"'.constjustData={type: 'SpecB'};constcastLater=<SpecA>justData;// Expected an error to be thrown, but the compiler accepts this.
Expected behavior:
The typecast to castLater should trigger a compile error.
Actual behavior:
it doesn't.
The text was updated successfully, but these errors were encountered:
justData gets type { type: string } so the downcast to { type: 'SpecA' } is OK. It gets that type (rather than { type: 'SpecB' }) so that it's not illegal to write e.g. `justData.type = 'whatever'". This logic doesn't apply in the earlier cases because a "fresh" object literal is known to not be mutated (or need to be able to be mutated) between when it's created and when it's bound to the variable it's initializing.
TypeScript Version: 2.1.5
Expected behavior:
The typecast to
castLater
should trigger a compile error.Actual behavior:
it doesn't.
The text was updated successfully, but these errors were encountered: