简体   繁体   English

如何使用两个描述“get”函数的typescript 0.9.5接口?

[英]How to consume two typescript 0.9.5 interfaces that both describe a “get” function?

I made this example of what seems to be valid typescript (playground link and inline as well): http://www.typescriptlang.org/Playground/#src=interface%20I1%20%7B%0A%09%09get(name%3A%20%22templatePath%22)%3A%20string%3B%0A%09%09get(name%3A%20string)%3A%20any%3B%09%0A%7D%0A%0Ainterface%20I2%20%7B%0A%09%09get(name%3A%20%22baseClass%22)%3A%20string%3B%0A%09%09get(name%3A%20string)%3A%20any%3B%0A%7D%0A%0Ainterface%20I3%20extends%20I1%2C%20I2%20%7B%0A%09%0A%7D%0A%0Aclass%20C3%20implements%20I3%20%7B%0A%09%09get(name%3A%20string)%20%7B%0A%09%09%09return%20name%3B%0A%09%09%7D%09%0A%7D 我做了一个看似有效的打字稿(操场链接和内联)的例子: http ://www.typescriptlang.org/Playground/#src=interface%20I1%20%7B%0A%09%09get(name %3A%20%22templatePath%22)%3A%20string%3B%0A%09%09get(名称%3A%20string)%3A%20any%3B%09%0A%7D%0A%0Ainterface%20I2%20%7B %0A%09%09get(名称%3A%20%22baseClass%22)%3A%20string%3B%0A%09%09get(名称%3A%20string)%3A%20any%3B%0A%7D%0A%0Ainterface %20I3%20extends%20I1%2C%20I2%20%7B%0A%09%0A%7D%0A%0Aclass%20C3%20implements%20I3%20%7B%0A%09%09get(名称%3A%20string)% 20%7B%0A%09%09%09return%20name%3B%0A%09%09%7D%09%0A%7D

interface I1 {
        get(name: "templatePath"): string;
        get(name: string): any; 
}

interface I2 {
        get(name: "baseClass"): string;
        get(name: string): any;
}

interface I3 extends I1, I2 {

}

class C3 implements I3 {
        get(name: string) {
            return name;
        }   
}

And I get an error indication types of property 'get' are not identical in these two interfaces. 我得到一个错误指示属性'get'的类型在这两个接口中不相同。 (1) I don't see how that matters and (2) I don't know how to get around this. (1)我不明白这是怎么回事;(2)我不知道怎么解决这个问题。

Specifically my interface looks like this: 具体来说,我的界面如下所示:

interface IGotoPane extends dijit._WidgetBase, dijit._TemplatedMixin, dijit._WidgetsInTemplateMixin {
    inputCoordinates: dijit._Widget;
}

I've found that I can eliminate the compiler errors by re-declaring several methods: 我发现我可以通过重新声明几种方法来消除编译器错误:

interface I3 extends I1, I2 {
        get(name: string): any;     
}

Or in the case of my specific issue, 或者就我的具体问题而言,

interface IGotoPane extends dijit._WidgetBase, dijit._TemplatedMixin, dijit._WidgetsInTemplateMixin {
    inputCoordinates: dijit._Widget;
    get(name: string): any;
    set(name: string, value: any, raiseChangeEvent?: boolean): void;
    set(values: Dojo.PropertiesMap): void;
    watch(prop: string, callback: Dojo.WatchCallback<any>): Dojo.WatchHandle;
}

Why is this necessary? 为什么这有必要?

The error about the properties needing to be identical is sort of common-sense -- what is the type of I3.get if left unspecified as in your first example? 关于需要相同的属性的错误是一种常识 - 如果在第一个示例中未指定,那么I3.get的类型是什么? There isn't any notion of "merging" function types in TypeScript, so it isn't clear what exactly this would mean. 在TypeScript中没有任何“合并”函数类型的概念,因此不清楚这究竟是什么意思。

You can reason about simple cases, but it quickly becomes very confusing for nontrivial cases, for example: 你可以推理一些简单的案例,但很快就会让人感到困惑,例如:

interface I1 {
    foo(a: string, b: "y"): number;
    foo(a: string, b: string): any;
}

interface I2 {
    foo(a: "x", b: string): boolean;
    foo(a: string, b: string): any;
}

interface I3 extends I1, I2 {
}

var x: I3;
var y = x.foo('x', 'y'); // y: number? boolean? any?

The fix of explicitly declaring get in I3 is the correct one. I3中明确声明get的修复是正确的。 You can copy down the constant-specialized signatures if they apply in that case. 如果在这种情况下适用,则可以复制常量专用签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM