2017-06-20 106 views
1

我想不通为什么我得到了这样的错误:接口签名不匹配

export interface IGrid { 
    (gridCell: GridCell): boolean 
} 

在我的课堂我有

foo(gridCell: GridCell): boolean { 
    return true; 
} 

错误:

Class 'X' incorrectly implements interface 'IGrid'. Type 'X' provides no match for the signature '(gridCell: GridCell): boolean'

更新:

我已经为接口的gridFormat签名添加了一个参数。

export interface IGrid { 
    gridFormat(gridCell: GridCell, x: number): boolean 
} 

类:

gridFormat(gridCell: GridCell): boolean { 
    return true; 
} 

现在的问题是没有错误,类没有实现与x: number放慢参数的功能。我怎样才能让界面正确地要求功能。

回答

2

IGrid接口为function interface,这意味着接口描述的功能。你可以这样实现它:

let yourFunc: IGrid = (gridCell: GridCell): boolean => { 
    return true; 
}; 

如果你想实现它在类的界面或许应该申报class type interface与函数成员:

export interface IGrid { 
    foo(gridCell: GridCell): boolean 
} 

class Grid implements IGrid { 
    foo(gridCell: GridCell): boolean { 
     return true; 
    } 
} 

回复:为什么没有错误时,实施中缺少接口定义的参数:

这是由设计。见this issueTypeScript FAQ

+0

好吧,这工作,但我还是不能让我的接口工作如何我想它。我已经更新了这个问题。签名不匹配,但没有错误。 –

+0

@el_pup_le这实际上是由设计。参见[此](https://stackoverflow.com/questions/35541247/typescript-not-checking-function-argument-types-declared-by-interfaces)和[此](https://github.com/Microsoft/ TypeScript/wiki/FAQ#why-are-functions-with-less-parameters-assignable-to-functions-that-take-more-parameters) – Saravana

+0

谢谢,这很令人沮丧。我看你是C#的人,你知道C#是否被设计成这样吗? –