Search code examples
typescripttypesimporttsconfig

IDE doesn't find the declared global types


I have a global typing file where I define my types, variables etc...

Now the project structure is like this:

trunk
   typings
     index.d.ts
   src
     Example.ts
     Example.d.ts
   tsconfig.json

In index.d.ts I have let's say

declare type userInfo = {
    username: string,
    password: string,
}

But in Example.d.ts when I use userInfo directly IDE says it cannot find the name whereas the tsc compiler shows no error.

declare class Something {
   ...
   getUserInfo: () => userInfo; // <--- this is highlighted red
}

The funny thing is that when I use userInfo in Example.ts there is no highlighted errors.

Another funny thing is when I go to the declaration it jumps to correct line in index.d.ts

I don't import the types in neither files as they are global types.

My tsconfig file is like below:

{
    "compilerOptions": {
        ...
        "typeRoots": ["./typings"],
        ...
    },
    ...
}

What may be the problem?


Solution

  • You should not declare types. Declare is useful when you want to indicate that there is some javascript class in global variable scope (and hence, Typescript is not able to see it). e.g. :

    declare class UserInfo {...} // typings.d.ts
    

    Now what you should use instead is just regular type

    type UserInfo = {...} // Example.ts
    

    Even better, I'd suggest to you use interface in your case, because the UserInfo type seems to be simple:

    // Example.ts
    interface UserInfo {
      username: string,
      password: string,
    } 
    

    Feel free to ask me if you don't get something