You can create a definition file, ending with the .d.ts
extension, and place it in your project where you’d like:
user.d.ts
interface IUser {
name: string,
mail: string
}
This is also a good way to fill missing global types. I have a lib/global
folder per project to place these files.
This only works with type definitions, not actual code, as (1) that actual code would have to be imported some way, (2) .d.ts
is ambient. Also, for types defined this way to be globally visible, the corresponding declaration file should not contain top-level exports (otherwise the declaration file will be regarded as a module, requiring an import to access its types).
You can also declare modules:
declare module "my-module" {
export declare class Something {
public something: number;
}
}
And then TypeScript will allow the following:
import { Something } from "my-module";