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

118
Visualizações
Type annotations in typescript
interface Test {
   name: string;
   age: number;
}

const subjectObj = {
   name: "Harhsit Singh",
   age: 17,
   height: "6'1",
};

const testObj: Test = subjectObj; //? NO ERROR

const testObj2: Test = {
   name: "Harhsit Singh",
   age: 17,
   height: "6'1", //! ERROR
};

I have created a interface test as shown in the picture, and a test subject named subjectObj and it contains more properties than the type test contains.

Now I have assigned my subjectObj to constant testObj of type test, then declared another constant testObj2 with the same type test and assigned it the same object, but this time initialized it with a new object of same structure as subjectObj.

But now it gives me an error that 'height' does not exist in type 'test', but didn't gave any error when i passed subjectObj as a reference to testObj though it also contains the property height.

I don't know why is this happening, so need some detailed explaination.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Excess property checking happens only when you pass object literals—when you pass in types, you run into the fact that TS never checks for exact types, and assumes that an object with excess types is what you mean.

That's really owing to the JS culture TS is embedded in. It would be really hard to code in TS against JS interfaces otherwise.

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