我有下面的TypeScript声明,它产生了令人困惑的结果。
class C<T extends {}> {
method() {
type X = T extends {} ? true : false;
// ^? type X = T extends {} ? true : false;
// Why is X not `true`?
}
}
游戏场
它将T视为一个未知实体,我很难从中获得任何有用的类型信息。
例如,尝试检查属性类型也不起作用:
class C<T extends { foo: number }> {
method() {
type X = T['foo'];
// ^? type X = T['foo'];
// Why is X not `number`?
}
}
奇怪的是,当需要为类型赋值时,类型会得到正确的求值:
const a: X = 5 // works correctly const b: X = "str" // fails correctly
这是怎么回事?