Typescript import / as vs import / require? [英] Typescript import/as vs import/require?
问题描述
$ b $ b
对于消费模块,TypeScript手册显示以下语法:
import express = require('express'); code>
但是, typescript.d.ts
文件显示:
import * as express fromexpress;
我也搜索MSDN博客,但找不到任何东西。
截至2016年初,哪一个更正确?如果有的话,两者之间有什么区别?
找到最新使用语法的信息的最佳来源在哪里,以便将来可以找到这些信息?
这些大都是等价的,但 import *
有一些限制 import ... = require
不。
import * as
创建一个模块对象的标识符,强调对象。根据ES6规范,该对象永远不可调用,或者新的
能够 - 它只具有属性。如果你想导入一个函数或类,你应该使用
import express = require('express');
或(取决于您的模块加载器)
express的import express
尝试使用 import *作为express
和然后根据ES6规范调用 express()
总是违法的。在某些运行时+透明环境中,这可能会发生任何事情,但是在未来的任何时候都可能会发生任何警告,这将使您感到难过。
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?
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 new
able - 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.
这篇关于Typescript import / as vs import / require?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!