microsoft / microsoft/TypeScript
JSDoc-typed node modules require special configuration in consumers to be useful
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
It looks like an ongoing goal for TS is to recommend JSDoc as the recommended way to give type-checking to raw JS users. As a user I am not a huge fan of this approach but I'll try to be objective in this issue.
If I write a node module js-lib as raw javascript typed with jsdoc, it will appear as untyped to consumers of my module unless they specifically opt in to JS type checking.
Example:
+-js-lib
| +-index.js
| +-package.json
|
+-ts-consumer
+-index.ts
+-package.json
+-tsconfig.json
js-lib/index.js
/**
* @param {string} s
* @returns {number}
*/
module.exports = function test(s) { return parseInt(s) };
js-lib/package.json
{
"name": "js-lib",
"main": "index.js"
}
ts-consumer/index.ts
import test = require('../js-lib');
// index.ts(1,23): error TS6143: Module '../js-lib' was resolved to '/Users/jarrad/src/personal/ts-test/lib/js-index.js', but '--allowJs' is not set.
import test = require('js-lib'); // after npm install ../js-lib
// index.ts(1,23): error TS7016: Could not find a declaration file for module 'lib'. '/Users/jarrad/src/personal/ts-test/lib/index.js' implicitly has an 'any' type.
// Try `npm install @types/lib` if it exists or add a new declaration (.d.ts) file containing `declare module 'lib';
test("test");
ts-consumer/tsconfig.json
{
"compilerOptions": {
"strict": true
}
}
It should be possible for me to make my js-lib usable to Typescript authors without them having to configure their own project with allowJs and checkJs.
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
Reproduce el problema usando js-lib/index.js, js-lib/package.json, ts-consumer/index.ts y ts-consumer/tsconfig.json del ejemplo. Empieza rastreando cómo el compilador resuelve un módulo Node de JavaScript y descubre la información de tipos para un consumidor de TypeScript. Se considera terminado cuando un módulo tipado con JSDoc puede proporcionar tipos útiles sin que los consumidores habiliten allowJs y checkJs.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, node.js, typescript
- Área
- compilers, developer-experience, tooling
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100