microsoft / microsoft/TypeScript
Suggestion: Type-check statement to verify type assignability
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ả
Search Terms
type check statement compile (compilation) time
Suggestion
I wanted to check compatibilities for types from different (especially external) modules in compilation time.
import { SomeType } from 'some-external';
// ... something statement here to check if SomeType is not changed
// by update for 'some-external' or etc.
To solve it, I suggest to add type-check statement like following:
// Compile error if 'SomeType' does not satisfy the constraint 'WantedType'
// (similar to error TS2344)
type assert SomeType extends WantedType;
// The message "Unexpected 'SomeType'" will be output if the error occurs
// - The trailing expression (message clause) must be a valid string literal
// (the message clause is not necessary for this suggestion
// but would make easier to solve the error...)
type assert SomeType extends WantedType, "Unexpected 'SomeType'";
- The type-check process should be same as the process for type constraints in type parameters.
- There is already
typestatement, but the above statements are not conflicted becausetypestatements requires=after BindingIdentifier (or TypeParameters). - Similar to #10421, but this
type assertonly check types; it does not cast variables. extendskeyword may confuse to one used in conditional type. (not ambiguous?)- For
type assert A extends B, bothAandBshould accept conditional type.
- For
- (
type assertis somewhat inspired by other languages'assertstatements (especiallystatic_assertin C/C++), but I don't know whether the wordstype assertare the best...)
Use Cases
- Check compatibilities for types simply and easily
- Useful to avoid using incompatible package versions with another packages/modules.
- Assert types explicitly with more human-readable
- This would not be useful for compilers/implementers, but would be useful for developers to read codes.
- Test type definition (e.g. for unit test)
Currently we can check types statically as following:
import { SomeType } from 'some-external';
// Helper definition to check type
type TypeCheckerOfWantedType<T extends WantedType> = T;
// Error if 'SomeType' does not satisfy 'WantedType'
type CheckResultForSomeType = TypeCheckerOfWantedType<SomeType>;
// this is necessary to avoid "'CheckResultForSomeType' is declared but never used"
declare global { var _dummyVariableForCheckSomeType: CheckResultForSomeType; }
While no JavaScript codes are generated from this code, this is more complex to check.
Examples
// Must not be an error
type assert Element extends Node;
// Error in 'es5', pass in 'es2015'
type assert ObjectConstructor extends { assign: (...args: any[]) => any; },
'Object.assign() is not available';
import { User } from 'db-library';
// Error if User does not have 'id' member with type assignable to 'number'
type assert User extends { id: number; }, 'Unexpected User type';
const a = someFunction();
if (typeof a === 'string') {
// treat 'a' as 'string'
} else {
// Error if narrowed type of 'a' does not have '{ data: string }'
type assert typeof a extends { data: string; };
// (this assert does not mean that type of 'a' is treated as '{ data: string }' here)
}
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
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 việc xem xét cú pháp type assert được đề xuất, hành vi của các ràng buộc kiểu, các trường hợp kiểu điều kiện và sự tương tác với các câu lệnh type hiện có. So sánh đề xuất này với kiến trúc kiểm tra kiểu và phân tích cú pháp hiện tại, sau đó xác định các kiểm thử cho những assertion được chấp nhận, lỗi khả năng gán, thông báo tùy chọn và các biểu thức có kiểu được thu hẹp; công việc được xem là hoàn tất khi cú pháp đã được đặc tả và hành vi của nó được bao phủ.
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