TS7016:找不到模块'../moduleName.js'的声明文件。 ...具有任何隐式类型

时间:2019-12-05 08:08:58

标签: typescript .d.ts

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

// 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"
};

1 个答案:

答案 0 :(得分:0)

解决了一些重构问题。关键的变化似乎是将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";