小编Nic*_*asB的帖子

如何防止泛型类型将“any”作为其类型参数?

如何防止泛型类型作为any其类型参数?如果我的通用参数受到使用extends关键字的限制,那么它可以被替换为奇怪的感觉any

据我了解,一切都延伸any,但any应该延伸任何东西(除了它本身)。any

这是一个例子:

type OneOrTwo = 1 | 2;
type MyType<T extends OneOrTwo> = T;
const t1: MyType<OneOrTwo> = 1; // OK
const t2: MyType<any> = 2;      // OK
const t3: MyType<OneOrTwo> = 3; // Error: Type '3' is not assignable to type 'OneOrTwo';
const t4: MyType<any> = 4;      // OK?? How can I prevent this?
Run Code Online (Sandbox Code Playgroud)

我可以让打字稿阻止我t4这样打字吗?要么通过更改代码,tsconfig要么使用选项?

typescript tsconfig typescript-generics

6
推荐指数
1
解决办法
1238
查看次数

标签 统计

tsconfig ×1

typescript ×1

typescript-generics ×1