Global types in typescript

前端 未结 3 1808
囚心锁ツ
囚心锁ツ 2020-12-09 15:25

Is there a way to make a file in your typescript file that defines globally accessible types?

I like typescript but find that when i want to be truly type safe I hav

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 16:08

    A little late but, you can add a file.d.ts anywhere in your project as I've noticed, and it will be picked up.

    For example, in my project I wanted a:

    Optional = T | null;
    

    And I didn't know where to add it, so I added a common.d.ts in a folder, and added:

    declare type Optional = T | null;
    

    Now it's being picked up and no errors. Didn't even need to import it. This of course being tested in vscode, not sure if it will work in your editor.

    (Depending on your file include/exclude rules of course, but most projects include all *.ts)

提交回复
热议问题