microsoft / microsoft/TypeScript
Relax visibility rules for type-aliases when 'declaration' compiler option is set.
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ả
One common thing I use type-aliases for is to give shorter, more expressive names to commonly used types throughout a file.
This is especially useful for callback signatures that see common use throughout a module, as well as string-literal types used in a union type, like type HttpMethods = 'get' | 'post' | 'put' | 'delete'.
Let's look at the following simplified example:
type CharCallback = (c: string, i: number) => string;
export function mapOnChar(str: string, fn: CharCallback): string {
const newStr = [];
for (let i = 0, lim = str.length; i < lim; i++)
newStr.push(fn(str.charAt(i), i));
return newStr.join('');
}
When using the declaration compiler option, this example fails to compile with the following error:
error TS4078: Parameter 'fn' of exported function has or is using private name 'CharCallback'.
This initially makes sense; the type-alias is not being exported so it is private. However, if you look at the type-alias, the only thing "private" about it is the name it was given, and an alias' name is not really important to or needed for a definition file.
There is no reason that the un-exported type-alias in the example cannot be automatically de-aliased back into (c: string, i: number) => string and that used in its place when the definition file is emitted.
However, if the alias in the example were to be exported, then it should not be de-aliased in the definition file.
I should point out that this suggestion is considering type-aliases only. If the CharCallback type was re-written as interface CharCallback { (c: string, i: number): string }, then that would be a good case to raise an error. An interface is generally considered more "concrete" than a simple type-alias, and so its name should be preserved and used in the definition file.
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
Bắt đầu với ví dụ TypeScript đã được báo cáo, sử dụng một bí danh CharCallback không được export, một hàm được export và tùy chọn trình biên dịch declaration. Theo dõi việc phát sinh declaration và kiểm tra khả năng hiển thị TS4078; được xem là hoàn tất khi các bí danh không được export có thể được inline trong các declaration được phát sinh, trong khi các bí danh được export vẫn giữ tên và trường hợp interface vẫn bị từ chối.
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
- 35/100