Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
175
rated 0 times [  179] [ 4]  / answers: 1 / hits: 189577  / 8 Years ago, mon, february 29, 2016, 12:00:00

I am using TypeScript with Express/Node.js.


For consuming modules, the TypeScript Handbook shows the following syntax:


import express = require('express');

But also the typescript.d.ts file shows:


import * as express from "express";

I also searched the MSDN blog but could not find anything.


Which one is more correct as of early 2016? What are the differences between the two, if any?


Where is the best source to find information on the latest syntax to use so I can find this information in the future?


More From » node.js

 Answers
8

These are mostly equivalent, but import * has some restrictions that import ... = require doesn't.



import * as creates an identifier that is a module object, emphasis on object. According to the ES6 spec, this object is never callable or newable - it only has properties. If you're trying to import a function or class, you should use



import express = require('express');


or (depending on your module loader)



import express from 'express';


Attempting to use import * as express and then invoking express() is always illegal according to the ES6 spec. In some runtime+transpilation environments this might happen to work anyway, but it might break at any point in the future without warning, which will make you sad.


[#63108] Saturday, February 27, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelynncherokeeg

Total Points: 697
Total Questions: 109
Total Answers: 104

Location: France
Member since Thu, Mar 18, 2021
3 Years ago
jaelynncherokeeg questions
Thu, May 27, 21, 00:00, 3 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
Wed, Sep 18, 19, 00:00, 5 Years ago
;