Global types in typescript

前端 未结 3 1806
囚心锁ツ
囚心锁ツ 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条回答
  •  我在风中等你
    2020-12-09 16:15

    Yes this is possible. You can find all information here: https://www.typescriptlang.org/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html

    The important part is this:

    declare global {
        /*~ Here, declare things that go in the global namespace, or augment
         *~ existing declarations in the global namespace
         */
        interface String {
            fancyFormat(opts: StringFormatOptions): string;
        }
    }
    

提交回复
热议问题