Sunday, 28 October 2018

Use type definitions from .d.ts file without importing

I am migrating a web-application from plain Javascript to Typescript, and am compiling all separate files into a single one using the --outFile compiler option and /// <reference path="..."/> directives.

This is nice, because I can split my code up into multiple files without having to worry about a browser supporting import.

The one library I use is color-js, and it has type definitions inside a file called color.d.ts.

To use it with plain Javascript, I do the following:

index.html:

[...]
<script src="scripts/color-js/color.js"></script>
<script src="main.js"></script> 
[...]

main.js/main.ts

let Color = net.brehaut.Color;
[...]

At runtime, this also works fine with Typescript, but during compilation, I get errors like these:

scripts/cue.ts(4,13): error TS2304: Cannot find name 'net'. 
scripts/cue.ts(25,18): error TS2304: Cannot find name 'Color'. 
scripts/cue.ts(26,16): error TS2304: Cannot find name 'Color'.` 
scripts/cue.ts(27,19): error TS2304: Cannot find name 'Color'. 
scripts/main.ts(839,52): error TS2304: Cannot find name 'Color'. 
scripts/main.ts(1019,20): error TS2304: Cannot find name 'Color'. 
scripts/main.ts(1022,16): error TS2304: Cannot find name 'Color'.

Is there a way to use the type definitions in color.d.ts only to define the type for compile-time, without importing it as a module?

As soon as I import it as one, I can't use --outFile anymore and I'd also have to use something like RequireJS, which I didn't get to work. Plain ES6 or ES5 imports aren't supported by my browser.

I thought maybe /// <reference types="..." would do that job, but that seems to be used for something else.



from Use type definitions from .d.ts file without importing

No comments:

Post a Comment