Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在typescript中导入库的不同方法有什么不同?_Typescript - Fatal编程技术网

在typescript中导入库的不同方法有什么不同?

在typescript中导入库的不同方法有什么不同?,typescript,Typescript,我在使用带有typescript的外部库时遇到了不少困难。我非常想为没有可用打字的库添加自定义打字,我很难理解导入和要求如何使用打字 区别是什么?在以下情况下,相应的键入如何查找: import something=require('something') import*作为来自“某物”的某物 const something=require('something') 从'something'导入{something}require是导入外部模块的“旧”方法,导入语法是当前推荐的与ES6语法一致的

我在使用带有typescript的外部库时遇到了不少困难。我非常想为没有可用打字的库添加自定义打字,我很难理解导入和要求如何使用打字

区别是什么?在以下情况下,相应的键入如何查找:

import something=require('something')

import*作为来自“某物”的某物

const something=require('something')


从'something'导入{something}

require
是导入外部模块的“旧”方法,
导入
语法是当前推荐的与ES6语法一致的方法。根据
tsconfig.json
TypeScript中配置的
module
,脚本将此语法转换为相关的模块类型/样式

如果您想了解TypeScript如何解析外部模块(它支持两种模式
node
classic
——可在
tsconfig.json
中配置为
moduleResolution
),请阅读以下文档:。主要区别在于如何解析非相对模块路径

如果您想了解TypeScript如何以及在何处查找类型(类型声明),请阅读以下博文:。从博客-它将:

  • 对于自定义代码,请尝试
    module.js
    旁边的
    module.d.ts
  • 尝试检查节点模块的
    键入:
    packages.json
    中(在
    节点模块中
  • 当存在
    tsconfig.json
    时,尝试在其处理的文件包中查找任何环境声明(
    declare module X
    语法)。这一点很重要,因为该文件的存在使TypeScript将所有包含的文件作为单个“项目”/“工作区”进行处理
  • 查找三重斜杠注释
    //
    ——考虑到可以使用
    tsconfig.json
    项目文件,我不建议再使用这种注释
  • 您还有两种类型的打字:

  • 外部模块键入-位于文件旁边,或者如果是节点模块,则在
    键入:
    project.json
    中存在指向它们的路径。语法是
    export X
    ,没有模块声明(文件是模块!)。考虑一下,它们被“绑定”到代码文件,类似于“这就是这个模块的样子”

  • 环境打字-生活在任何地方。语法是
    声明模块X
    。从“某处存在一个类型/模块X,这就是它的样子”的角度考虑。对于模块,TypeScript将根据模块名称进行匹配。其他用例是全局变量(例如jQuery的
    $

  • 有关可以在
    tsconfig.json
    中配置什么以及如何配置的文档,请阅读:

    关于您给出的不同示例,假设您有一个名为
    moduleA
    的模块。需要注意的是,文件是JS/TS世界中的一个模块,因此:

    模块a.ts

    export class Car
    {
        public model: string = "";
    }
    
    import moduleA = require("./moduleA");
    
    new moduleA.Car();
    
    // equivalent to the above - import everything under myModuleName
    import * as myModuleName from './moduleA';
    
    new myModuleName.Car();
    
    // import only the Car class
    import {Car} from './moduleA'
    
    new Car();
    
    // import only the Car class and alias it as MyCar
    import {Car as MyCar} from './moduleA'
    
    new MyCar();
    
    moduleA.js-传输(目标ES5和模块类型CommonJS)

    main.ts

    export class Car
    {
        public model: string = "";
    }
    
    import moduleA = require("./moduleA");
    
    new moduleA.Car();
    
    // equivalent to the above - import everything under myModuleName
    import * as myModuleName from './moduleA';
    
    new myModuleName.Car();
    
    // import only the Car class
    import {Car} from './moduleA'
    
    new Car();
    
    // import only the Car class and alias it as MyCar
    import {Car as MyCar} from './moduleA'
    
    new MyCar();
    
    main.js-传输(目标ES5和模块类型CommonJS)