BaldEagle
BaldEagle

Reputation: 1018

TypeScript: How can I get a clean compile?

I hope this is simple!

TypeScript Version 1.9.0-dev.20160512 (available from npm install -g typescript@next and recommended by @basarat) Node v5.11.0 Windows 10.0.10586

file 1, u1c.ts

import * as u1u from "./u1u.ts"
let p = u1u.Promise
console.log(`from u1c.ts`)

file 2, u1u.ts

console.log(`from u1u.ts`)    
export let Promise = require(`bluebird`) //http://bluebirdjs.com

file 3, tsconfig.json (relevant part only); I want to write in ES6 and get output in ES3 (on the presumption that all browsers out there will know about it).

{
    "compileOnSave": true,
    "complierOptions": {
        "lib": "ES6",
        "target": "ES3"
    }
}

result 1

>tsc u1c.ts
u1u.ts(2,22): error TS2304: Cannot find name 'require'.

Can't find "require"? Sounds like it doesn't know about Node.js. Let's teach it! typings gives me access to a fifth version of node.d.ts last changed 2016-05-09; I pulled it in and pointed at it.

new u1u.ts

/// <reference path="./typings/node/node.d.ts" />  // <==== new line, only change
console.log(`from u1u.ts`)    
export let Promise = require(`bluebird`) //http://bluebirdjs.com

result 2 (same as result 1)

>tsc u1c.ts
u1u.ts(2,22): error TS2304: Cannot find name 'require'.

I'm out of ideas. How can I get a clean compile?

Thanks in advance for any time and ideas you offer!

Upvotes: 0

Views: 5264

Answers (1)

basarat
basarat

Reputation: 275937

Can't find "require"? Sounds like it doesn't know about Node.js

Definitely a good idea to include node.d.ts. However you should use import/require instead of var/require (or let/require, const/require). The difference is that import tells TypeScript that its is a module. (more on modules)

/// <reference path="./typings/node/node.d.ts" /> // <==== new line, only change

The issue is the path is wrong. Suspect you need main (or browser) in there.

PS: Recommend using tsconfig.json (https://basarat.gitbooks.io/typescript/content/docs/project/tsconfig.html) with exclude to rule out the version of typings you don't want.

Upvotes: 1

Related Questions