Closed
Description
TypeScript Version: 3.0.1
Search Terms: "Type string is not assignable to type", narrow types, object literal, string literal
Code
declare function f<T>(arg: T ): void
f({ 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'
type Player = 'black' | 'white'
type WinCondition =
| { type: 'win', player: Player }
| { type: 'draw' }```
const checkWin = (someArg: number): Option<WinCondition> => {
return some({ type: 'draw' })
// ^-- Type '{ type: string; }' is not assignable to type '{ type: "draw"; }'
}
Related Issues: #26413