【问题标题】:How to import moment types definitions如何导入矩类型定义
【发布时间】:2017-09-10 15:55:37
【问题描述】:

我使用这个 yeoman 生成器制作了一个带有 typescript 的 angular 1 项目:https://github.com/FountainJS/generator-fountain-systemjs
它使用 SystemJS 和 jspm 来获取依赖关系,但使用 npm 来定义类型(使用 distinctlyTyped 存储库)。

这几天我一直在努力尝试导入 moment 类型定义。 我已经使用 jspm 安装了 moment,我发现它带有自己的类型定义,所以如果你调用命令 npm install @types/moment --save-dev 你只会得到一个存根和一个弃用警告

这是 Moment 的存根类型定义 (https://github.com/moment/moment)。 Moment 提供了自己的类型 定义,所以你不需要安装@types/moment!

现在,我不知道这是我的编辑器、项目或打字稿设置还是真的打字稿问题,但我似乎无法正确导入时刻的类型定义。

如果我在 import moment from 'moment';import * as moment from 'moment'; 上执行此操作,我的编辑器上会出现此错误(atom with atom-typescript,但我在 Visual Studio Code 上也有同样的错误)Cannot find module 'moment'.

尽管有这个错误,当我构建我的应用程序时它工作正常(调用时刻函数有效)。

我已经尝试了很多我在互联网上找到的解决方案(显然,从现在开始导入类型定义是一个常见问题)但没有一个奏效。 昨天我设法通过在node_modules/@types 中手动创建目录moment 并将moment.d.ts 放入其中(不过我不得不将其重命名为index.d.ts)来使其工作。

因为我不是很喜欢这个解决方案,所以我想至少创建一个可以放入内容的类型文件夹,而无需修改 node_modules 结构。所以我创建了一个文件夹custom-types 并将带有类型定义的moment 文件夹放在那里,然后将custom-types 添加到tsconfig.json 中,我认为这会很好,但实际上错误再次出现......

现在我没有想法了,我真的不知道还能尝试什么。

这是我当前的tsconfig.json(在最后一次尝试中,我添加了具有不同路径的custom-types 文件夹,尽管它与node_modules 文件夹和tsconfig.json 文件处于同一级别)

{
    "compilerOptions": {
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "module": "system",
        "target": "es5",
        "moduleResolution": "classic",
        "typeRoots": [
            "node_modules/@types/",
            "./custom-types/",
            "custom-types/",
            "../custom-types/",
            "../../custom-types/",
            "../../../custom-types/"
        ],
        "types": [
            "angular-ui-router",
            "angular",
            "angular-mocks",
            "jquery",
            "jasmine",
            "moment",
            "es6-shim"
        ]
    },
    "compileOnSave": false,
    "filesGlob": [
        "custom-types/**/*.ts",
        "src/**/*.ts",
        "src/**/*.tsx",
        "!jspm_packages/**",
        "!node_modules/**"
    ]
}

【问题讨论】:

    标签: typescript momentjs atom-editor


    【解决方案1】:

    我一直在 TypeScript (IntelliJ) 中使用moment,没有额外的工作来为其安装类型。

    以下是相关配置:

    • 内部tsconfig.json
      • "moduleResolution": "Node"
      • 根本没有"types" 部分
    • ./package.json 没有 @types/moment
    • 内部./node_modules/moment/
      • ./moment.d.ts 存在
      • ./package.json"typings": "./moment.d.ts"
      • ./package.json"version": "2.14.1"

    当我切换回 "moduleResolution": "Classic" 时,TypeScript 会显示 cannot find module 'moment'。所以这可能是罪魁祸首。

    【讨论】:

    • 很高兴听到!有时,为tsconfig.json 找到正确的设置就像解决魔方一样。
    • 谢谢! "moduleResolution": "node" 起到了部分作用,但就我而言,我还必须设置 "allowSyntheticDefaultImports": true
    • 原来将allowSyntheticDefaultImports 设置为true 并添加import moment from 'moment'; 破坏了我的其他一些requirejs 代码。使用import moment = require('moment'); 代替它可以正常工作(仍然需要"moduleResolution": "Node")。
    • @Worthy7 干杯
    • 谢谢罗伯特。我的配置是相同的,除了:1)我有一个 types 部分(其值为 ['core-js', 'hammerjs']),2)我有 ./node_modules/moment/package.json 版本 2.22.1 。我也不知道correct import syntax,但import * as moment from 'moment' 为我工作。
    猜你喜欢
    • 1970-01-01
    • 2019-04-30
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 2018-09-05
    • 2019-08-17
    相关资源
    最近更新 更多