microsoft / microsoft/TypeScript

Proposal: Export multiple, runtime-specific declaration files

Aperta
#63,641 0 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Proposal Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

🔍 Search Terms

export "multiple declaration file" "node bun deno" "custom conditions"

✅ Viability Checklist
⭐ Suggestion

The feature request is to, as a library publisher, have capabilities to export multiple declaration files for each supported runtime/platform.

Consumers also need capabilities to choose which declaration file should be used by their compilation process.

With the proliferation of JavaScript Runtimes other than Node.js such as Bun, Deno, Bare and Cloudflare Workers, libraries will tend to be more and more agnostic or “multi-runtime”.

Each platform has slightly different Type dependencies, they may have different names, is also possible that they may have different ways to be imported.

Suggestions for exporting multiple versions:

Using the package.json's exports:

{
  "name": "my-package",
  "version": "1.0.0",
  "exports": {
    ".": {
      "types": {
        "bun": "./index.bun.d.ts",
        "deno": "./index.deno.d.ts",
        "default": "./index.d.ts" // node.js typings
      },  
      "default": "./index.js"
    }
  }
}

Similar alternative using imports as in https://nodejs.org/api/packages.html#subpath-imports:

{
  "name": "my-package",
  "version": "1.0.0",
  "exports": {
    ".": {
      "types": "./index.d.ts" // which would import '#types'
      "default": "./index.js"
    }
  },
  "imports": {
    "#types": {
      "bun": "./index.bun.d.ts",
      "deno": "./index.deno.d.ts",
      "node": "./index.node.d.ts"
    }
  }
}

Alternative that reuses typesVersions, but using custom conditions as keys:

{
  "name": "package-name",
  "version": "1.0.0",
  "types": "./index.d.ts",
  "typesVersions": {
    "bun": { "index.d.ts": ["index.bun.d.ts"] },
    "deno": { "index.d.ts": ["index.deno.d.ts"] },
  }
}

Suggestions for consuming a specific version:

My first suggestion is to reuse the types option feature to also trigger the proposed conditional mechanism defined at package.json.

{
  "compilerOptions": {
    "types": ["deno"]
  }
}

Another alternative is to use reuse the custom conditions feature to also trigger the proposed conditional mechanism:

{
  "compilerOptions": {
    "customConditions": ["deno"]
  }
}
📃 Motivating Example

A practical example is: https://github.com/mafintosh/streamx/pull/123, streamx supports Node.js and Bare Runtime, and here are two versions of the same library that I'd like to expose both:

https://github.com/yassernasc/streamx/blob/17526106a3bd11fd177f9ce62250c19577f4ab64/index.bare.d.ts

https://github.com/yassernasc/streamx/blob/17526106a3bd11fd177f9ce62250c19577f4ab64/index.node.d.ts

💻 Use Cases
  1. What do you want to use this for?

As a library publisher, I want consumers to have realistic typings whenever is the supported runtime consuming the library.

  1. What shortcomings exist with current approaches?

I have not found a direct solution to the problem.

  1. What workarounds are you using in the meantime?

I've been writing a minimal runtime.d.ts declaration file that defines a minimal subset that all covered runtimes implement. This approach duplicates preexisting typings from the runtimes typings, however, the runtime.d.ts file tends to be unmaintained and outdated in the long run, plus, adds unwanted noise to the repositories.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando gli esempi di exports, imports e typesVersions di package.json, insieme alle opzioni compilerOptions e customConditions menzionate nell’issue. Confronta i percorsi proposti per la selezione dei file di dichiarazione e l’esempio motivante di streamx. Il lavoro sarà considerato completato quando saranno definiti un design condiviso e un piano di implementazione per la pubblicazione e il consumo di dichiarazioni specifiche del runtime.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, typescript
Ambito
developer-experience, tooling
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.