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
functionexample(parameter: {value: "a"}){// Function requires argument with string literalreturn;}varmyObject={value: "a"// TS infers value to be string here, causing problems passing this object to function};example(myObject);// Error: Argument of type '{ value: string; }' is not assignable to parameter of type 'ObjectType'. Types of property 'value' are incompatible. Type 'string' is not assignable to type '"a"'.
Expected behavior:
No compiler error
Actual behavior:
Compiler error (see self-contained example)
The text was updated successfully, but these errors were encountered:
I realize workaround exists for this (just specify type explcitly for myObject), but one of my colleagues ran into this issue while using 3rd party library and was puzzled by this behavior.
Since workarounds are possible, this probably is not a high priority issue, but maybe there's some possibility to explore more flexible type inference for TypeScript project.
This is working as intended. Literal types are widened when they are inferred for mutable locations (such as the value property in your example) unless you have a type annotation or type assertion indicating otherwise.
TypeScript Version: 2.3.4
Code
Expected behavior:
No compiler error
Actual behavior:
Compiler error (see self-contained example)
The text was updated successfully, but these errors were encountered: