Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
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

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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.

In your case, because the functions have no parameters and return a string, they are compatible.

TS Playground

type NumFn = (n: number) => string;
declare const isCompatible: (() => string) extends NumFn ? true : false; // true
about 4 years ago · Santiago Trujillo Relatório

0

If you invoke (x:number)=>string without passing in x, you get 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. When you assign ()=>string to (x:number)=>string, the compiler ask: can ()=>string behave the same as (x:number)=>string?

i.e. can ()=>string takes in a number and spit out a string, just like what (x:number)=>string do?

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

about 4 years ago · Santiago Trujillo Relatório

0

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)

Function parameters are checked one at a time, with the type in each corresponding parameter position checked against each other. 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. Here, also, the return type of our function expression is implied by the values it returns typescript handbook

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

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda