microsoft / microsoft/TypeScript

Allow local .js/.ts files to be typed by their corresponding .d.ts file

Aperta
#29,056 12 commenti 7 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Suggestion

If I have source/index.js which has:

function isObject(value) {
	// null is object, hence the extra check
	return value !== null && typeof value === 'object'
}

And then I supply a source/index.d.ts file which has:

/** Checks to see if a value is an object */
export function isObject(value: any): boolean

And I update package.json:types to source/index.d.ts.

Then I should not receive the following error in source/index.js:

[ts] Parameter 'value' implicitly has an 'any' type. [7006]
(parameter) value: any

And I should get complete intellisense.

Use Cases

This approach has the following benefits:

  1. Source files can run without compilation, this has been incredibly useful for @bevry's hundreds of packages, which are all written in jsdoc'd esnext that runs on mostly node 4 and above, with compilation only for older node versions (but most of the time, it is our source code that is running, thanks to editions)
  2. JSDoc types do not provide intellisense nor type checking for typescript consumers
  3. TypeDoc for documentation seems nicer than JSDoc, especially with overloads
  4. Going all in on TypeScript may be too much to ask for, for many module authors - as a typescript project requires types for all its dependencies, as well as accurate (rather than merely simplistic) mappings, which some of the time, is just too inconvenient to bother with - such as this, as well as eachr which a pure typescript conversion seems impossible to me (after days of effort, as typescript always whines about incorrect types)

Alternatives to this would be:

  1. Have typescript projects support intellisense and type checking for jsdoc js files
  2. Automatically generate .d.ts files for jsdoc js files

For now, the best workaround seems to be:

  1. Spend weeks upon weeks converting your hundreds of modules to typescript to satisfy typescript's need for types for everything
  2. Provide jsdoc .js files, and manual .d.ts files

Examples

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

I haven't filled out this checklist as I do not know enough about typescript's inner workings to be able to fill it out.

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 con l’esempio in source/index.js e con l’esempio corrispondente in source/index.d.ts, quindi esamina come viene interpretata la voce types di package.json. Il lavoro è completato quando il file JavaScript locale utilizza il file di dichiarazione per la tipizzazione dei parametri e IntelliSense senza l’errore implicit-any, mantenendo invariato il comportamento di runtime esistente.

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

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
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.