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
There seems to be a problem with unions and literal types when both types of the union contain a field with the same name.
TypeScript Version: 2.5.2
Code
The small example
classA{field: 42;}classB{field: number;otherfield: number;}// Gives an errorletx: A|B={field: 42};// Type '{ field: number; }' is not assignable to type 'A | B'.// Type '{ field: number; }' is not assignable to type 'B'.// Property 'otherfield' is missing in type '{ field: number; }'.
The simple example of a more realistic situation (typeorm-ish code)
typeOrderBy='ASC'|'DESC';typeSimpleOrderByCondition={name?: OrderBy;randomField?: OrderBy;};typeOrderByCondition=SimpleOrderByCondition|(()=>SimpleOrderByCondition);// Works OK as expectedletorderBy1: OrderByCondition={randomField: 'ASC'};// gives an error. But, it should work.letorderBy2: OrderByCondition={name: 'ASC'};
Expected behavior:
No error
Actual behavior:
[ts]
Type '{ name: string; }' is not assignable to type 'OrderByCondition'.
Type '{ name: string; }' is not assignable to type '() => SimpleOrderByCondition'.
Type '{ name: string; }' provides no match for the signature '(): SimpleOrderByCondition'.
The text was updated successfully, but these errors were encountered:
classA{field: number;//change type to number and it will be working}classB{field: number;otherfield: number;}letx: A|B={field: 42};//OK, because type of {field:42} is not {field:42} it is {field:number},
Second case is more interesting.
It is the same as above but name is a intrinsic property of function object, The type with property name looks like an candidate for Function type but not the same because it has only name property.
Non-lossy deduplication of the constituent type set may expect all properties of Function to be in object literal.
When a literal is contextually typed by it is base type (number, string, or an enum) we should still infer the literal type. I am sure i have seen this reported before but can not seem to find the issues to reference here.
There seems to be a problem with unions and literal types when both types of the union contain a field with the same name.
TypeScript Version: 2.5.2
Code
The small example
The simple example of a more realistic situation (typeorm-ish code)
Expected behavior:
No error
Actual behavior:
The text was updated successfully, but these errors were encountered: