TypeScript支持重载字符串参数,以便在使用某些参数调用时返回any
的方法可以正确键入。为什么在重载字符串参数时必须在实现之前有一个通用类型声明?
这是在规范中定义在两个地方: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#1.8 https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.9.2.4
然而,让这些正常工作是很困难的。这是一个示例类,它有一个通用的get
。当您将字符串"a"
或"b"
传递给此函数时,我想提供特定类型,在所有其他情况下,返回类型为any
。
我包含两个专门的签名,然后提供一个通用签名,然后提供具有一般签名的实现。以下代码正确地报告前两个分配为x
和y
的错误,但如果我删除了一般签名(get(name: string): any
),则会出现错误:Argument of type '"c"' is not assignable to parameter of type '"b"'.
为什么除了实现上的签名外,还需要一般签名?
export default class Example {
contents: any
get(name: "a"): number
get(name: "b"): string
// Why is this required???
get(name: string): any
get(name: string): any { return this.contents[name] }
}
let w = new Example()
// Expected errors
// Type 'number' is not assignable to type 'string'.
let x: string = w.get("a")
// Type 'string' is not assignable to type 'number'.
let y: number = w.get("b")
// I get an error here only if I remove the general signature before the
// implementation of get.
// Argument of type '"c"' is not assignable to parameter of type '"b"'.
let z: string[] = w.get("c")