【问题标题】:TS7016: Could not find a declaration file for module '../moduleName.js'. ... has implicit any typeTS7016:找不到模块“../moduleName.js”的声明文件。 ...隐含任何类型
【发布时间】:2020-03-30 02:57:55
【问题描述】:

我在尝试导入分配给对象的模块时看到此错误:

// keys.js
export default {
  SHOPIFY_API_KEY: "removed"
  // more keys removed
}

// globalTypings.d.ts
declare module 'nonce' {
 export default function nonce(length?: number): string
}

declare module "keys" {
 export default interface keys {
  SHOPIFY_API_KEY : string
  , SHOPIFY_API_SECRET : string
  , SCOPES : string
  , CLIENT_APP_URL : string
 }
}


// index.ts
import keys from "../keys.js";
// TS7016: Could not find a declaration file for module '../keys.js'. 'removed/keys.js' implicitly has an 'any' type

nonce 模块正在工作。我只包括它,以防多个声明是禁止的。上面我推测的语法错误的根源是什么?

**也试过了**

import * as keys from "../keys.js";

同样的错误结果

import {keys} from "../keys.js";
// and default keyword is removed from the interface declaration

同样的错误。

// inside the d.ts file
declare module "keys" {
 export interface keys {
  SHOPIFY_API_KEY : string
  , SHOPIFY_API_SECRET : string
  , SCOPES : string
  , CLIENT_APP_URL : string
 }
}

// TS1039: Initializers are not allowed in ambient contexts.
const keyObj: keys = {
 SHOPIFY_API_KEY : "val"
 , SHOPIFY_API_SECRET : "val"
 , SCOPES : "val"
 , CLIENT_APP_URL : "val"
};

【问题讨论】:

  • 你试过在你的模块声明中使用removed/keys.js吗?
  • @AlanBirtles 我写了一个类似于 keys.js 中的虚拟对象,并用结果更新了帖子

标签: typescript .d.ts


【解决方案1】:

通过一些重构解决了。关键变化似乎是将keys.js切换到keys.ts,因此它可以接受来自global.d.ts文件的接口:

// global.d.ts
declare module "keys" {
 interface KeysInterface {
  SHOPIFY_API_KEY : string
  , SHOPIFY_API_SECRET : string
  , SCOPES : string
  , CLIENT_APP_URL : string
 }

 export default KeysInterface;
}

// keys.ts
const keysObj = {
  SHOPIFY_API_KEY: "removed"
  , SHOPIFY_API_SECRET: "removed"
  , SCOPES: "removed"
  , CLIENT_APP_URL: "removed"
};

export default keysObj;


// index.ts
import keys from "../keys";

【讨论】:

    猜你喜欢
    • 2021-07-24
    • 2019-10-22
    • 1970-01-01
    • 2019-01-09
    • 2021-12-20
    • 1970-01-01
    • 2017-05-18
    • 2017-05-08
    相关资源
    最近更新 更多