简体   繁体   English

混淆打字稿中接口的用法

[英]Confuse about the usage of Interface in typescript

All: 所有:

I am pretty new to typescript, when I try typescript handbook, there are two type(function and class) Interface definitions: 我对打字稿很陌生,当我尝试打字稿手册时,有两种类型(函数和类)接口定义:

For function, it says: 对于功能,它说:

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

For class, it says: 对于课堂,它说:

interface ClockInterface {
    currentTime: Date;
    setTime(d: Date);
}

class Clock implements ClockInterface  {
    currentTime: Date;
    setTime(d: Date) {
        this.currentTime = d;
    }
    constructor(h: number, m: number) { }
}

What confuses me here is: 让我感到困惑的是:

Why there are diff ways to define function inside Interface, in the Function Types section it says: 为什么在“接口类型”部分中有定义方法的差异方法,在“函数类型”部分中说:

To describe a function type with an interface, we give the interface a call signature. 为了描述带有接口的函数类型,我们给接口一个调用签名。 This is like a function declaration with only the parameter list and return type given. 这就像只声明参数列表和返回类型的函数声明。

Why both works when define function? 为什么在定义函数时两者都起作用? Or the second one( setTime(d: Date); part ) is not a function definition? 还是第二个(setTime(d:Date); part)不是函数定义?

Thanks 谢谢

An interface is a contract that describes things the implementing entity must implement. 接口是描述实现实体必须实现的事物的契约。 You provide a function signature in a class interface as a way of saying: "If you are implementing this interface, then your class must contain this function and accept these arguments. 您可以在类接口中提供函数签名,作为一种说法:“如果要实现此接口,则您的类必须包含此函数并接受这些参数。

The other interface you showed is a whole interface just for a function. 您显示的另一个接口是仅用于功能的整个接口。 This way you can pass this function somewhere that accepts the SearchFunc interface. 这样,您可以在接受SearchFunc接口的某个地方传递此函数。 As long as your function implementing that interface accepts the same parameter types (doesn't need to be same variable names) then the requirements are satisfied. 只要实现该接口的函数接受相同的参数类型(不必是相同的变量名),就可以满足要求。

For example: 例如:

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

function doStuff (searchUtility: SearchFunc) {
  var contentExists = searchUtility(textToSearch, 'query text');
}

Whatever you pass to doStuff needs to adhere to the SearchFunc interface argument types. 无论您传递给doStuff什么,都必须遵守SearchFunc接口参数类型。 doStuff is declaring a contract that says "If you give me a function, this is how it must look. It must accept a string of text to search, a string of text to query for, and it must return a boolean. doStuff正在声明一个合同,上面写着“如果您给我一个函数,它必须是它的外观。它必须接受要搜索的文本字符串,要查询的文本字符串,并且必须返回布尔值。

doStuff(function (srcText: string, queryText: string) {
  return srcText.indexOf(queryText) !== -1;
}); 

The logic in the function that actually does the searching isn't specified in the contract. 合同中未指定实际执行搜索的函数中的逻辑。 Only what parameter types are accepted and what type the return value should be. 仅接受哪些参数类型,返回值应为哪种类型。


So basically a class interface lets you define how an entire class should look, including any methods on that class (methods are functions attached to objects). 因此,基本上,类接口使您可以定义整个类的外观,包括该类上的任何方法(方法是附加到对象的函数)。 A function interface just acts as a type so that some other API could expect that type and you'd have to satisfy it. 函数接口只是充当一种类型,以便其他一些API可以期望该类型,而您必须满足它。 If all this hypothetical API is a function that looks a certain way then it would be overkill to make it depend on a whole class containing only one function definition. 如果所有这些假设的API都是一个以某种方式看起来的函数,那么将其依赖于仅包含一个函数定义的整个类将是过大的杀伤力。

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

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