typescriptcommonjsuse-stricttypescript1.7

Why is typescript complaining when a /// reference is after 'use strict'?


This question is some what related to Why is typescript failing to import a module?, which I asked yesterday. The initial setup is the same.

I have a simple typescript file like this:

/// <reference path="./typings/js-yaml/js-yaml.d.ts"/>
'use strict';
import * as y from 'js-yaml';
console.log(y);

When I compile like so tsc --module commonjs file.ts, typescript is happy.

But, when I move the /// reference below the 'use strict', like this:

'use strict';
/// <reference path="./typings/js-yaml/js-yaml.d.ts"/>
import * as y from 'js-yaml';
console.log(y);

Typescript is not happy:

$ tsc --module commonjs file.ts 
file.ts(4,20): error TS2307: Cannot find module 'js-yaml'.

Typescript does indeed output a compiled file and it is the same as the one output originally, except of course that the /// reference is after 'use strict' in the second case.

What is happening here?


Solution

  • From MSDN,

    The following rules apply to a reference directive. The reference XML comment must be declared before any script.

    This may be the reason.