Home > Software engineering >  Angulare interfaces - one cannot "see the other"
Angulare interfaces - one cannot "see the other"

Time:04-12

This might be something basic, but I am a beginner, and a colleague who doesn't work with me anymore left something in the code which behaves differently.

I have 4 interfaces.ts files below

1. interfaces.ts

interface Pro{

id: number;

title: string;

}

2. interfaces.ts

interface Tar{

id: number;

title: string;

pros: Pro[];

}

As you can see Tar uses an array of Pro-s. Works fine

3. interfaces.ts

export interface Pay{

id: number;

title: string;

}

4. interfaces.ts

interface Prm{

id: number;

title: string;

pays: Pay[];

}

The editor is adding red underline under this last line (pays: Pay[];) saying cannot find name 'Pay'. The suggested fix is an import { Pay } , but that is something that was not needed when we used 1 in 2. Using 3 in 4 might not work because of the "export interface" ?

If I am using the suggested fix, then the whole module 4 (prm) looks like it is braking the whole project, ng build crashes in 50 places

I am not sure what to change, as the guy who made the whole module 3 and interface, made this one different than all the rest of the modules.

thank you

CodePudding user response:

You do need to import any classes or interfaces that are not in the same file. No exceptions. Any class or interface you will import must be made public by prepending export to the class or interface name.

I can't say why your editor didn't complain about using Pro in Tar without importing it. It may be thinking that Pro is a built-in class from typescript, or angular core, or something you ARE importing.

But the main thing to keep in mind, is that editors often make mistakes when showing you red lines. The only way to know for sure if it's broken is to run ng build.

Hope that helps.

CodePudding user response:

So basically what I did was

  1. add export to the Prm too

  2. add import { Pay } '...' at the beginning

This snowballed into crashing everywhere, where Prm was used. So I have to decide if I will use export everywhere, or nowhere.

  • Related