typescript - Finns det ett sätt att deklarera ett specifikt modulnamn för en genererad .d.ts-fil (i ett JavaScript-projekt)?


Finns det ett sätt att deklarera ett annat modulnamn för en genererad .d.ts-fil?

tsc genererar declare module "index" { istället för declare module "@myorg/my-pkg" (vilket skulle matcha name-attributet i package.json).

Observera: Detta är i ett rent JavaScript-projekt där jag försöker generera typer.

/* Generated with tsc --init via TypeScript 3.7.0-beta */
{
  "compilerOptions": {
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
     "allowJs": true,                         /* Allow javascript files to be compiled. */
     "declaration": true,                     /* Generates corresponding '.d.ts' file. */
    "outFile": "./lib/index.js",              /* Concatenate and emit output to single file. */
    "emitDeclarationOnly": true,              /* Only emit .d.ts files. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
  },
  "include": [
    "js/**/*"
  ]
}

Svar #1

Tyvärr kan typer inte skrivas ut till en enda fil direkt. Det finns ett öppet förslag om att lägga till "type bundling" (#4433) för att lägga till funktionen. Det finns också tredjepartsbibliotek som kan hjälpa till med detta.

En annan lösning, om du är okej med att flera filer skapas, är att specificera outDir istället för outFile. Detta var lösningen jag valde, genom att sätta "outDir": "./types" i tsconfig.json och sätta "types": "./types/index.d.ts" i package.json. Detta fungerade som jag förväntade mig när jag importerade modulen till andra projekt.

Relaterade artiklar