typescript - TS7016: Kunde inte hitta en deklarationsfil för modulen '../moduleName.js'. ... har implicit typen any
Jag ser detta fel när jag försöker importera ett modul som är tilldelat ett objekt:
// 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-modulen fungerar. Jag inkluderar den bara ifall flera deklarationer är ett no-no. Vad är källan till mitt antagliga syntaxfel ovan?
**Försökte också**
import * as keys from "../keys.js";
Samma felresultat
import {keys} from "../keys.js";
// and default keyword is removed from the interface declaration
Samma fel.
// 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"
};
Svar #1
Löst med viss omstrukturering. Den viktigaste förändringen verkade vara att byta keys.js till keys.ts så att det kunde acceptera gränssnittet från global.d.ts-filen:
// 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";
