microsoft / microsoft/TypeScript
[API] Ability to check if a parameter is being passed to const type parameter
Chưa có ai nhận issue này.
- 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ả
🔍 Search Terms
const type parameter
✅ Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
Hi from ts-api-utils.
We'd like to be able to check if an expression, passed as an argument of a call expression, is in a "const context". Checking for as const is fairly trivial as we can just search up the syntax tree, but checking for const type parameters seems impossible without using TypeScript internals.
With code examples:
We can currently check if foo is in a "const context" here fairly trivially:
const test1 = { foo: 1 } as const;
But can't do the same for foo in this example:
declare const fn: <const A>(param: A) => unknown;
const test2 = fn({ foo: 2 });
Proposal
Ideally, we would like an officially supported way to make such a check.
We currently have a PR in progress that works using ts internals, but we'd prefer not to rely on ts internals.
📃 Motivating Example
N/A
💻 Use Cases
-
What do you want to use this for?
determining whether a type is readonly
https://github.com/eslint-functional/eslint-plugin-functional/issues/827 -
What shortcomings exist with current approaches?
There doesn't seem to be anyway official way to do this -
What workarounds are you using in the meantime?
Use ts internals
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 bằng cách tái hiện ví dụ về tham số kiểu const và xem xét TypeScript compiler API cùng với cách tiếp cận nội bộ hiện có được sử dụng bởi ts-api-utils PR #519. Công việc được xem là hoàn tất khi một API được hỗ trợ chính thức có thể xác định liệu một biểu thức đối số có nằm trong ngữ cảnh const hay không, bao gồm trường hợp sử dụng readonly-type, mà không phụ thuộc vào các thành phần nội bộ của TypeScript.
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
- 30/100