简体   繁体   English

typescript 中的功能

[英]Functions in typescript

Why no missing error?为什么没有遗漏错误?

interface User {
  // way 1
  foo(): string;  
  foo2(x:number): string; 

  // way 2
  normal: () => string;
  normal2: (x:number) => string;
}

let user: User = {
  // way 1
  foo: () => '',
  foo2: () => '', // why no error since x is missing
  
  // way 2
  normal: () => '',
  normal2: () => '', // why no error since x is missing
};

See this Typescript Playground看到这个 Typescript 游乐场

A lower- arity function is assignable to a higher-arity one as long as its return type is compatible and the parameters which are present are compatible.只要它的返回类型兼容并且存在的参数兼容,可以将较低数量的 function 分配给更高数量的 function。

In your case, because the functions have no parameters and return a string , they are compatible.在您的情况下,因为函数没有参数并返回string ,所以它们是兼容的。

TS Playground TS游乐场

type NumFn = (n: number) => string;
declare const isCompatible: (() => string) extends NumFn ? true : false; // true

If you invoke (x:number)=>string without passing in x , you get An argument for 'x' was not provided.如果你调用(x:number)=>string而不传入x ,你会得到An argument for 'x' was not provided. error.错误。

BUT THIS IS NOT WHAT YOU ARE DOING但这不是你在做什么

What you are doing is assigning ()=>string to (x:number)=>string , which is valid.您正在做的是将()=>string分配给(x:number)=>string ,这是有效的。 When you assign ()=>string to (x:number)=>string , the compiler ask: can ()=>string behaves the same as (x:number)=>string ?当您将()=>string分配给(x:number)=>string时,编译器会询问: ()=>string的行为是否与(x:number)=>string相同?

ie can ()=>string takes in a number and spit out a string , just like what (x:number)=>string do?即可以()=>string接受一个number并吐出一个string ,就像(x:number)=>string一样吗?

The answer is yes, ()=>string technically can take in any number, but just ignoring it, then return a string, independent of what number it take in. Therefore, ()=>string is assignable to (x:number)=>string答案是肯定的, ()=>string在技术上可以接受任何数字,但只是忽略它,然后返回一个字符串,与它接受的数字无关。因此, ()=>string可分配给(x:number)=>string

Just like the answers above seems like it can only be detected when you call the function without arguments my bet is it is something to do with typescript contextual typing (I too am learning here)就像上面的答案似乎只有在没有 arguments 的情况下调用 function 时才能检测到它,我敢打赌这与 typescript 上下文类型有关(我也在学习这里)

Function parameters are checked one at a time, with the type in each corresponding parameter position checked against each other. Function 参数一次检查一个,每个对应参数 position 中的类型相互检查。 If you do not want to specify types at all, TypeScript's contextual typing can infer the argument types since the function value is assigned directly to a variable of type SearchFunc.如果您根本不想指定类型,TypeScript 的上下文类型可以推断参数类型,因为 function 值直接分配给 SearchFunc 类型的变量。 Here, also, the return type of our function expression is implied by the values it returns typescript handbook在这里,我们的 function 表达式的返回类型也隐含在它返回的值typescript 手册中

interface Counter {
  (start: number): string;
  interval: number;
  foo: (x:number) => string;
  bar(x:number):string;
  reset(): void;
}

function getCounter(): Counter {
  let counter = function (s...

Playground Link 游乐场链接

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

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