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

298
Visualizações
How to refer a interface that's used before it was defined?

I am defining TypeScript interfaces and landed in a situation where I need to define circular interface.

Eg. ISchool have IStudent and IStudent have ISchool, the problem is interface ISchool {} has IStudent which is used before it's defined. Also, I can't define interface IStudent before because IStudent has ISchool. How can handle this situation?

interface ISchool {
  id: string;
  name: string;
  students: IStudent[];
}

interface IStudent {
  id: string;
  name: string;
  school: ISchool;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Better not to have circular dependencies in general. If you create a school with students the students contain a school which also contains students which also contains a school etc. That's why I reccomend you to rewrite your code so your IStudent interface references the schools with its id.

export interface ISchool {
    id: string;
    name: string;
    students: IStudent[];
  }
  
export interface IStudent {
    id: string;
    name: string;
    school: string;
}

Now you can pass the id of the school to the IStudent so your IStudent interface is still linked with your ISchool interface.

Or you could disable the eslint rule by adding this to your .eslintrc.json. Because after all its jsut a lint error and javascript allows almost everything.

"no-use-before-define": "off"
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