TypeScript import/as vs import/require? [duplicate] Ask Question

TypeScript import/as vs import/require? [duplicate] Ask Question

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?

ベストアンサー1

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 objectES6仕様によれば、このオブジェクトは呼び出し可能でもnew可能でもなく、プロパティのみを持ちます。関数やクラスをインポートする場合は、

import express = require('express');

または(モジュールローダーによって異なります)

import express from 'express';

ES6 仕様によると、使用してimport * as expressから呼び出すことexpress()は常に違法です。一部のランタイム + トランスパイル環境では、これはとにかく機能する可能性がありますが、将来の任意の時点で警告なしに機能しなくなる可能性があり、残念なことになります。

おすすめ記事