Search code examples
typescripttypescript-typingsdefinitelytyped

What's the difference between using 'export declare function' and 'export function' in a '.d.ts' file


I've seen in typescript declaration files people write decelerations like so:

export function useTheme(): ITheme;

But I was under the impression that the declare keyword was necessary for writing type decelerations for functions that are defined elsewhere?

If this is valid, and so is export declare function useTheme(): ITheme;, then is there any reason to choose one or the other?


Solution

  • Making it simple, in code if you want to create some declaration like you know some function exists globally but compiler doesn't know that - you use declare keyword. But definition file is a little different. As this is actually definition file, nothing else but declarations should be there hence these files treat all entities as declarations no matter that you used declare keyword or not. Using declare keyword there is just kind of convention which is obviously recommended as we shouldn't specify content type based on extension in that case. Here TypeScript compiler knows that d.ts is a declaration file but some other compiler may not