Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript之接口(二) #17

Open
joinmouse opened this issue Aug 5, 2019 · 0 comments
Open

TypeScript之接口(二) #17

joinmouse opened this issue Aug 5, 2019 · 0 comments

Comments

@joinmouse
Copy link
Owner

额外属性检查

表示SquareConfig有任意数量的其他属性

interface SquareConfig {
    color?: string;
    width?: number;
    [propName: string]: any;
}

可索引的类型

我们希望可以描述那些能够“通过索引得到”的类型,比如a[10]ageMap["daniel"]
可索引类型具有一个索引签名,它描述了对象索引的类型,还有相应的索引返回值类型

interface StringArray {
  [index: number]: string;
}

let myArray: StringArray;
myArray = ["Bob", "Fred"];

let myStr: string = myArray[0];

当前的索引签名表示了当用number去索引StringArray时会得到string类型的返回值。

函数类型

使用接口表示函数类型,我们需要给接口定义一个调用签名。 它就像是一个只有参数列表和返回值类型的函数定义,参数列表里的每个参数都需要名字和类型。

interface SearchFunc {
  (source: string, subString: string): boolean;
}

let mySearch: SearchFunc;
mySearch = function(src: string, sub: string): boolean {
  let result = src.search(sub);
  return result > -1;
}

函数的参数会逐个进行检查,要求对应位置上的参数类型是兼容的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant