Initially, it's important to grasp that typescript instances recognize a file in either TWO modes: script mode or module mode
In script mode, everything is considered global. What does this mean exactly? For instance:
/**
* global.ts or any other name,
* you can place it ANYWHERE within your project
* The Typescript instance will ALWAYS locate it
**/
declare module '*.glsl' {
const value: string;
export default value;
}
So, how does a typescript instance determine if a file is global? The rule is fairly straightforward - if the file contains no export
or import
statements, it is considered global. Otherwise, it is not!
/**
import s from 'foo.glsl' from another file, will no longer work
Because there is no definition of *.glsl present
**/
declare module '*.glsl' {
const value: string;
export default value;
}
/**
* The export statement removes the "global" status from this file!
*/
export type T=string
What should one do if they want to utilize declaration files from others in their project without using import
, which would invalidate existing declarations?
The solution lies in using
/// <reference types="declaration.d.ts" />
, which serves as a form of
import
while preserving the file's "globalness."
In reality, placing declaration.d.ts
anywhere in the project renders <reference />
unnecessary, as it is inherently global, just like any files lacking export
/export
The use of <reference />
proves especially advantageous when the desired declaration file is located in node_module where it is not visible by default