【问题标题】:Add custom type typescript 3添加自定义类型打字稿3
【发布时间】:2020-03-18 15:08:45
【问题描述】:

当前行为:我从 @types/openlayers": "4.6.17" 迁移到 "@types/ol": "5.3.6" 并且在 ol/render 中找不到函数 getVectorContext(event)所以我手动将它添加到 render.d.ts 但下一次 npm install 我将丢失此修改

想要的行为:我想将包含自定义类型的文件 *.d.ts 添加到我的项目中,但出现错误:找不到模块无法解析'/pathCustomTypes'

【问题讨论】:

    标签: typescript types


    【解决方案1】:

    您不需要@types/openlayers@types/ol,只需使用ol 包。然后做:

    import { getVectorContext } from 'ol/render';
    

    编辑:

    我不太确定您的评论。但是,如果您在 node_modules/ol 中打开 README.md,您可以阅读:

    ol 包包含一个带有 JSDoc 注释的 src/ 文件夹 来源。 TypeScript 可以从这些来源获取类型定义 jsconfig.json项目根目录下的配置文件:

    "compilerOptions": {
        "checkJs": true,
        // Point to the JSDoc typed sources when using modules from the ol package
        "baseUrl": "./",
        "paths": {
          "ol": ["node_modules/ol/src"],
          "ol/*": ["node_modules/ol/src/*"]
        }   },   "include": [
        "**/*.js",
        "node_modules/ol/**/*.js"   ] }
    

    具有此配置的项目模板:https://gist.github.com/9a7253cb4712e8bf38d75d8ac898e36c

    请注意,上述内容仅在使用纯 JavaScript 进行创作时有效。为了 在 TypeScript 项目中与 tsconfig.json 类似的配置, 您的里程可能会有所不同。

    【讨论】:

    • 感谢您的回答和您的解决方案,但我们没有使用 typescript 所有类型的优势的问题是任何';所以我不明白它是如何工作的,以及编译器如何知道 ol lib,我问我们是否可以从打字和使用 ol 6 中获益。
    • @Ayoub 不客气 :) 随时接受我的回答。
    猜你喜欢
    • 1970-01-01
    • 2021-08-20
    • 2021-12-01
    • 2015-01-01
    • 2020-05-07
    • 1970-01-01
    • 2019-01-02
    • 2019-07-12
    • 2018-01-16
    相关资源
    最近更新 更多