microsoft / microsoft/TypeScript

Allow minimal type checking of JavaScript files

Đang mở
#28,448 7 bình luận 21 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Domain: JavaScript In Discussion Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

I'm in the process of introducing TypeScript support into an enormous, 6-year old web application with a lot of JavaScript files. In order to get the most advantages out of TypeScript while we slowly migrate over (which will likely happen slowly), I'd like the following to be true:

  • TypeScript files (.ts and .tsx) are type checked with strict: true, so that we get as much useful feedback about TS code as possible.
  • JavaScript code that imports and then incorrectly uses typed TypeScript code throws a TS error
  • Other TypeScript errors in JavaScript files are suppressed, because we haven't had the chance to migrate the file to TypeScript yet. OR, at the least, JavaScript files are type checked with strict: false to minimize the number of errors they output.

Primary Suggestion

It would be fantastic if typescript had a "minimalCheckJs" config option that, when set to true, would only check JS files for errors on imported typed code. For example, setting "minimalCheckJs": true, "strict": true in tsconfig.json would have this effect:

# moduleA.ts
function hello(msg) {} # throws "no implicit any" error on "msg" arg

export default function logA(msg: string) {
  console.log(msg);
}

# moduleB.js
import logA from './moduleA'
import something from './someJavaScriptFile.js' # does not throw "cannot find module" error

logA(1) # throws TS error, because logA is typed

function logB(msg) { # does not show "no implicit any" error on "msg" arg
  console.log(msg);
}

This feature would allow me to convert a file from JS to TS, add types to the file, and immediately be assured that the exported code is being used correctly throughout my entire code base (including JS files). Currently, I can set "checkJs": true, but then I will see thousands of other kinds of errors that TypeScript finds in the JS files, such as cannot find module errors.

Alternative Suggestion

If the above feature is difficult to implement, a less ideal but also helpful feature would be to allow setting "strict": false for JS files and "strict": true for TS files. Some way to combine the following:

# strictly type check TS files
{
  "compilerOptions": {
    "checkJs": false,
    "allowJs": true,
    "strict": true
  },
  "include": ["**/*.ts", "**/*.tsx"],
}

# type check JS files with strict: false
{
  "compilerOptions": {
    "checkJs": true,
    "allowJs": true,
    "strict": false
  },
  "include": ["**/*.js", "**/*.jsx"],
}

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách kiểm tra cách các tùy chọn trong tsconfig.json như checkJs, allowJs và strict được diễn giải đối với các tệp JavaScript và TypeScript. Vì issue không xác định tệp nguồn, bài kiểm thử hoặc điểm vào của trình biên dịch nào, trước tiên hãy lần theo cách xử lý hiện có của checkJs và strict. Công việc được xem là hoàn tất khi JavaScript báo cáo các lỗi từ mã TypeScript có kiểu được import, đồng thời loại bỏ các chẩn đoán JavaScript không liên quan, với phạm vi bao phủ cho cấu hình được yêu cầu.

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, typescript
Lĩnh vực
compilers
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

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.