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
{{ message }}
Repository navigation
String literal types aren't inferred from object literals when using generics #26621
Search Terms: "Type string is not assignable to type", narrow types, object literal, string literal
Code
declarefunctionf<T>(arg: T): voidf({someKey: 'C'})// someKey's type is (incorrectly) inferred as 'C'f<{someKey: 'C'}>({someKey: 'C'})// someKey's type is (correctly) inferred as 'C'
Expected behavior:
In the first function call, someKey's type should be 'C'.
Actual behavior: someKey' s type is inferred as string. I don't know if this is a bug or a feature request, but when not using generics, this does not happen (see this playground link). This is especially problematic when using discriminated unions. Here's the actual code that prompted me to open this issue:
import{Option,some,none}from'fp-ts/lib/Option'typePlayer='black'|'white'typeWinCondition=|{type: 'win',player: Player}|{type: 'draw'}```
constcheckWin=(someArg: number): Option<WinCondition>=>{returnsome({type: 'draw'})// ^-- Type '{ type: string; }' is not assignable to type '{ type: "draw"; }'}
Activity
ahejlsberg commentedon Aug 23, 2018
Duplicate of #11152.