microsoft / microsoft/TypeScript
JSDoc-typed node modules require special configuration in consumers to be useful
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 2 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 132
Mô tả
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.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Tái hiện sự cố bằng cách sử dụng js-lib/index.js, js-lib/package.json, ts-consumer/index.ts và ts-consumer/tsconfig.json từ ví dụ. Bắt đầu bằng cách lần theo cách trình biên dịch phân giải một module Node JavaScript và phát hiện thông tin kiểu cho một consumer TypeScript. Công việc được xem là hoàn tất khi một module được định kiểu bằng JSDoc có thể cung cấp các kiểu hữu ích mà không yêu cầu các consumer bật allowJs và checkJs.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- javascript, node.js, typescript
- Lĩnh vực
- compilers, developer-experience, tooling
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 25/100