In the below type definitions why do P1
and P2
have different values?
type P1 = (() => 22) extends {[k:string]:any} ? 1:2 //`P1 == 1`
type P2 = (() => 22) extends {[k:string]:unknown} ? 1:2 //`P2 == 2`
In the below type definitions why do P1
and P2
have different values?
type P1 = (() => 22) extends {[k:string]:any} ? 1:2 //`P1 == 1`
type P2 = (() => 22) extends {[k:string]:unknown} ? 1:2 //`P2 == 2`
See Breaking Change: { [k: string]: unknown }
is no longer a wildcard assignment target for an authoritative answer.
The index signature {[k: string]: any}
behaves specially in Typescript: it's a valid assignment target for any object type. Usually types like () => 22
are not given implicit index signatures, and so it is an error to assign () => 22
to {[k: string]: unknown}
. Only when the property type is any
do you get the special permissive behavior:
let anyRec: { [k: string]: any };
anyRec = () => 22; // okay
let unkRec: { [k: string]: unknown };
unkRec = () => 22; // error
So that's why P1
and P2
differ.