【问题标题】:Import js module to typescript file (normalizr)将 js 模块导入打字稿文件(normalizr)
【发布时间】:2017-10-22 05:22:10
【问题描述】:

我正在尝试将normalizr 库用于我的打字稿项目。所以我定义了以下实体架构(js格式)

//schema.js
import { Schema, Entity } from "normalizr";
export const user = new Schema.Entity("users", options = { idAttribute: "userId" });

并尝试在 .ts 文件中使用它

//app.ts
import { user } from "./schemas";

导致错误:

module ./schemas 被解析为 "schemas.js" 但 "allowJs" 参数 没有设置

如果我在 tsconfig.json 中设置 allowJs = true 则会出现错误:

无法写入文件“.../schemas.js”,因为它会覆盖输入 文件

我也用this approach

//schemas.ts
import * as normalizr from 'normalizr';
export const user = new normalizr.Schema.Entity("users");

但是又出现了错误:

类型 'typeof' 上不存在属性架构 .../normalizr/index

我该如何解决?

Visual Studio 2017,ts v.2.2.2

【问题讨论】:

    标签: typescript normalizr


    【解决方案1】:

    查看 normalizr 的类型定义,Entity 类位于 schema 命名空间内(带有小写的 s):

    import * as normalizr from 'normalizr';
    export const user = new normalizr.schema.Entity("users");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-01
      • 1970-01-01
      • 2017-10-29
      • 2022-01-14
      • 2018-01-10
      • 1970-01-01
      • 2020-03-02
      • 2017-03-09
      相关资源
      最近更新 更多