microsoft / microsoft/TypeScript

Add `import type "mod"`

Đang mở
#36,812 12 bình luận 13 reaction 0 người được giao Xem trên GitHub

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

Awaiting More Feedback In Discussion Suggestion
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ả

Search Terms

type-only import

Suggestion

The new type-only import syntax supports most variations of import statement but not this one:
import type 'mod'
It may seem stupid because no type is actually imported?! But it's in fact useful if mod contains some global declarations that you need.

Global declarations are currently only imported from: typeroots (@types/* by default) or types configs; and referenced modules.

This syntax is even more useful because it can't be emulated today. If you do import 'mod' then typing works but TS assumes you import the module for side-effects and will not erase it.(EDIT: see triple-slash comment)

Use Cases

I encountered this today.
One package I use provides some dependency injection and basically defines the following module augmentation:
inject(x: 'myType'): MyType
which is a strongly typed overload of
inject<T = unknown>(x: string): T.
Inject itself comes from another library so code that wants to use this import needs to do this:

import { inject } from 'other-lib';
import type 'my-lib';

let my = inject('myType'); // correctly typed as MyType

Current work-arounds include:

  • Importing a type import { MyType } from 'my-lib' but if you don't use it in code it'll be a warning;
  • Import without type import 'my-lib' but it won't be erased;
  • Add this lib to typeroots or types and do nothing (might be better in some cases). (EDIT: not really working, see my comment below for an alternative solution.)
  • Use /// <reference types="mod" />, which works but I think most people have forgotten about? (I have 😜)

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.

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

Issue đề xuất hỗ trợ import type "mod" để tải các khai báo toàn cục mà không phát sinh mã ở runtime, với một ví dụ liên quan đến module augmentation. Issue không xác định các tệp triển khai hoặc test, vì vậy trước tiên hãy tìm các test cho import parser, type-checking và emit; hoàn thành có nghĩa là cú pháp vượt qua type-checking và tạo ra output đã được loại bỏ như dự kiến.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
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.