代码
/** @template T */
class C {
/** @param {T} t */
constructor(t) {
/** @type {Object<string, T> } -- does not instantiate T */
this.ot = { p: t }
/** @type {{ [s: string]: T }} -- instantiates T */
this.st = { p: t }
}
}
var c = new C(1)
c.ot.p // should have type number, has type T
c.st.p // has type number
预期行为:
c.ot.p : number
实际行为:
c.ot.p : T
Object<string, T>
的语法类似于类型别名,但它并没有像类型别名那样进行示例化,这可能是问题所在。
1条答案
按热度按时间kd3sttzy1#
哎呀,变量逃逸了它们的范围???我有点惊讶它没有产生某种类型的内部编译器错误。
有一个Playground测试用例:
编译器选项
**Playground链接:**提供