【发布时间】:2018-05-30 17:40:24
【问题描述】:
TypeScript 编译器tsc 编译以下代码没有任何问题,即使带有--strict 标志也是如此。但是,该代码包含一个基本错误,在 Java 或 C# 等语言中是可以避免的。
interface IBox<T> {
value: T;
}
const numberBox: IBox<number> = { value: 1 };
function insertString(items: IBox<string | number>): void {
items.value = 'Test';
}
// this call is problematic
insertString(numberBox);
// throws at runtime:
// "TypeError: numberBox.value.toExponential is not a function"
numberBox.value.toExponential();
可以配置tsc 以便识别这样的错误吗?
【问题讨论】:
标签: typescript generics types type-conversion tsc