Monday, August 31, 2015

TypeScript definition ambiguity woes. Which require function should go? RequireJS' or NodeJS'?

If you encountered this error:

Error:(49, 13) TS2403: Subsequent variable declarations must have the same type.  Variable 'require' must be of type 'Require', but here has type 'NodeRequire'.

It's better to remove RequireJS's require, as you can access the require functionality of RequireJS's through its syntax sugar a.k.a. simplified CommonJS wrapping.

RequireJS syntax sugar for require is done like this:
define(require => {
    var calculator : typeof SharedModel.Calculator = require('Calculator');
    var financialCalculator : typeof SharedModel.FinancialCalculator = require('FinancialCalculator');

On RequireJS's TypeScript definition, require.d.ts, comment or remove its require declaration:
// Ambient declarations for 'require' and 'define'
declare var requirejs: Require;
// declare var require: Require;
declare var define: RequireDefine;

No comments:

Post a Comment