Global types in typescript

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 really type safe, I have to explicitly import types from the whole system. This is pretty annoying.

+4
source share
1 answer

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

An important part:

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;
    }
}
+2
source

Source: https://habr.com/ru/post/1673048/


All Articles