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
interfaceFoo{bar: string;name: string;}constcommon: Partial<Foo>={bar: 'foobar'};constfoos: Foo[]=[{ ...common,name: 'one'},{ ...common,name: 'two'},];// errorconstfoosTwo: Foo[]=[{ ...<Foo>common,name: 'one'},{ ...<Foo>common,name: 'two'},];// type coercion works
Expected behavior:
Typescript knows all the properties are provided and there are no errors
Actual behavior:
[ts]
Type '{ name: string; bar?: string; }[]' is not assignable to type 'Foo[]'.
Type '{ name: string; bar?: string; }' is not assignable to type 'Foo'.
Property 'bar' is optional in type '{ name: string; bar?: string; }' but required in type 'Foo'.
This error does not occur when assigning to a class property instead of a const:
When you write a type annotation for a variable declaration, TypeScript validates that the initializer conforms to that type, but otherwise discards information about the initializer. So we later here:
TypeScript Version: 2.9.2
Search Terms: partial, spread, optional, required
Code
TS Playground
Expected behavior:
Typescript knows all the properties are provided and there are no errors
Actual behavior:
This error does not occur when assigning to a class property instead of a
const
:TS Playground
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: