microsoft / microsoft/TypeScript
Allow local .js/.ts files to be typed by their corresponding .d.ts file
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
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:
- 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)
- JSDoc types do not provide intellisense nor type checking for typescript consumers
- TypeDoc for documentation seems nicer than JSDoc, especially with overloads
- 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:
- Have typescript projects support intellisense and type checking for jsdoc js files
- Automatically generate .d.ts files for jsdoc js files
For now, the best workaround seems to be:
- Spend weeks upon weeks converting your hundreds of modules to typescript to satisfy typescript's need for types for everything
- Provide jsdoc .js files, and manual .d.ts files
Examples
- https://github.com/bevry/typechecker/tree/dev-typescript-dts
- https://github.com/bevry/eachr/tree/dev-typescript-dts
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con el ejemplo de source/index.js y el ejemplo correspondiente de source/index.d.ts; después, inspecciona cómo se interpreta la entrada types de package.json. El trabajo estará terminado cuando el archivo JavaScript local use el archivo de declaración para el tipado de los parámetros y IntelliSense sin el error implicit-any, mientras el comportamiento existente en tiempo de ejecución permanezca sin cambios.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100