Support for all type features in declaration files.
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 15/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- typescript
- Lĩnh vực
- compilers
Hướng nghiên cứu
Không có tệp nào trong repository, bài kiểm thử hoặc điểm vào của phần triển khai được nêu. Trước tiên, hãy thu hẹp yêu cầu vào một hạn chế cụ thể của việc phát sinh declaration, chẳng hạn như TS4094 hoặc TS4060, sau đó xác định hành vi liên quan của compiler và định nghĩa các bài kiểm thử đầu ra declaration thể hiện tính tương đương cho tính năng đó.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
I'm on TS 3.7.3 at time of writing this.
It seems like this issue should simply be focused on and fixed before continuing to add more and more features to the language and widening the source vs declaration gap.
This is quite a problem!
Search terms
"typescript declaration file limitations"
"has or is using private name"
"exported class expression may not be private or protected"
"Property 'foo' of exported class expression may not be private or protected. ts(4094)"
"Return type of exported function has or is using private name 'foo'. ts(4060)"
Related issues (a small fraction of the results on Google) in no particular order:
- https://github.com/microsoft/TypeScript/issues/29872 (@canonic-epicure)
- https://github.com/microsoft/TypeScript/issues/17744 (@fr0)
- https://github.com/Microsoft/TypeScript/issues/18791 (@johnnyreilly)
- https://github.com/microsoft/TypeScript/issues/30355 (@a-student)
- https://github.com/Microsoft/TypeScript/issues/17293 (@saschanaz)
- https://github.com/Microsoft/TypeScript/issues/24226 (@rjamesnw)
- https://github.com/gluons/vue-thailand-address/issues/5 (@gluons)
- https://github.com/types/sequelize/issues/176 (@billybarnum)
- https://github.com/microsoft/TypeScript/issues/6307 (@jeffschwartz)
- https://github.com/Microsoft/TypeScript/issues/23110 (@evil-shrike)
- https://github.com/typed-ember/ember-cli-typescript/issues/133 (@dfreeman)
- https://github.com/microsoft/TypeScript/issues/15947 (@ryansmith94)
- https://github.com/vuejs/vue/issues/6999 (@gluons)
- https://github.com/Microsoft/TypeScript/issues/16440 (@Manusan42)
- https://github.com/microsoft/TypeScript/issues/28754 (@trusktr)
- https://github.com/Microsoft/TypeScript/issues/5284 (@tommyZZM)
- https://github.com/Microsoft/TypeScript/issues/23280 (@mjbvz)
- https://github.com/gr2m/javascript-plugin-architecture-with-typescript-definitions/issues/1 (@gr2m)
- And the list on Google literally goes on and on and on. (I tried to skip any that were actual recognized bugs, fixed or not, but it may be possible I missed one or two, though the vast majority appear to be limitations of declaration files)
Workaround
One way to work around all of the above issues, for libraries, is to have library authors point their types field in package.json to their source entrypoint. This will eliminate the problems in the listed issues, but has some big downsides:
- If a downstream consumer of a library without declaration files (f.e. without
dist/index.d.ts) but withtypespointing to a source file (f.e.src/index.ts) then if the consumer'stsconfig.jsonsettings are not the same as the library's, this may cause type errors (f.e. the library hadstrictset tofalsewhile being developed, but the consumer setsstricttotrueand TypeScript picks up all the strict type errors in the library code).- This is documented in https://github.com/microsoft/TypeScript/issues/35744
- If
skipLibCheckis set to true, the consumer project's compilation will still type-check the (non-declaration) library code regardless.- This is documented in https://github.com/microsoft/TypeScript/issues/41883
With those two problems detailed, if you know you will be working in a system where you can guarantee that all libraries and consumers of those libraries will be compiled with the same compiler (tsconfig.json) settings (i.e. similar to Deno that defines compiler options for everyone), then pointing the package.json types field to a source file (because declaration output is not possible) is currently the workaround that allows all the unsupported features of declaration files to be usable. But this will fail badly if a library author does not know what compiler settings library consumers will have: everything may work fine during development and testing within external examples, but a downstream user will eventually report type errors if they compile their program with different settings while depending on the declaration-less libraries!
Suggestion
Support for all type features in declaration files.
Please. 🙏 I love you TS team, you have enabled so much better code dev with TypeScript. ❤️ 😊 TS just needs declaration love for support of all features of the language.
Some work is being done, f.e. https://github.com/microsoft/TypeScript/issues/23127, but overall I feel that too much effort is being put onto new language features while declaration output is left behind.
This creates a poor developer experience when people's working code doesn't work (the moment they wish to publish it as a library and turn on declaration: true).
I hope the amazing and wonderful TS team realize how frustrating it may feel for someone to work on a project for months, only to end with un-fixable type errors the moment they want to publish the code as a library with declaration: true, then having to abandon features and re-write their code, or try to point package.json "types" to .ts files only to face other issues.
I wish every new feature of the language came paired with working tests for equivalent declaration emit. Can this be made a requirement for every new language feature, just like unit tests are a requirement?
Use Case
Use any language feature, publish your code, then happily move along, without facing issues like
src/html/DeclarativeBase.ts:25:10 - error TS4094: Property 'onChildSlotChange' of exported class expression may not be private or protected.
25 function makeDeclarativeBase() {
~~~~~~~~~~~~~~~~~~~
src/html/WebComponent.ts:32:10 - error TS4060: Return type of exported function has or is using private name 'WebComponent'.
32 function WebComponentMixin<T extends Constructor<HTMLElement>>(Base: T) {
~~~~~~~~~~~~~~~~~
I worked hard for months to get the typings in the above code working, then I turned on "declaration": true and TypeScript said "today's not the day!".
I hope you the team can see that these issues are a pain, and realize the need to bring declaration emit to parity with language features and not letting the gap between language features and declaration output widen.
Examples
The issue that sparked me to write this was https://github.com/microsoft/TypeScript/issues/35744.
In that issue there's an example of what implicit return types might look like:
export declare function AwesomeMixin<T extends Constructor>(Base: T) {
type Foo = SomeOtherType<T> // only types allowed in here.
// only special return statements allowed, or something.
return declare class Awesome extends Base {
method(): Foo
}
}
We'd need solutions for other problems like the protected/private error above, etc.
Of course it'll take some imagination, but more importantly it will take some discipline: disallow new features without declaration parity.
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.
- 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
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.
Issue khác của microsoft/TypeScript
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
microsoft/TypeScript#64322 · 2 bình luận · 1 reaction · 2 người được giao ·
-
Possible Improvement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
microsoft/TypeScript#64278 · 1 bình luận · 1 reaction ·
-
Docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
microsoft/TypeScript#64118 · 1 bình luận ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
microsoft/TypeScript#64094 ·
-
Docs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
microsoft/TypeScript#63959 · 5 bình luận ·
Tất cả issue của microsoft/TypeScript
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
milvus-io/birdwatcher#545 ·
-
kind/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
kubernetes-sigs/prow#953 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
caddyserver/caddy#8046 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
jaegertracing/jaeger#9588 ·