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

259
Visualizações
Typescript error 2345 seems to be inconsistent across simple examples

I have a question about the basics of Typescript.

Consider this example:

// Interface 1
interface Empty {}

// Interface 2
interface Single {
    a: number;
}

function printEmpty(arg: Empty): void {
    console.log(`Hello World.`);
}

function printSingle(arg: Single): void {
    console.log(`Hello World, a: ${arg.a} `)
}

const obj = {a: 1, b: 2};

// ===================== Tests =====================

// TEST 1
printEmpty({a: 1, b: 2});  // valid

// TEST 2
// Q1: if the above is valid, why is this invalid
printSingle({a: 1, b: 2}) // invalid!

// TEST 3
// Q2: if the above is invalid, why is this valid
printSingle(obj);  // valid

The error I get is

Argument of type '{ a: number; b: number; }' is not assignable to parameter of type 'Single'. Object literal may only specify known properties, and 'b' does not exist in type 'Single'.(2345)

Why does TEST 2 fail but TEST 1 and TEST 3 pass?

Here's a reproduction of this error on the official TS playground: [LINK]

about 4 years ago · Juan Pablo Isaza
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