microsoft / microsoft/TypeScript
Issue with union type
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ả
TypeScript Version: 3.7.5
Expected behavior:
Type of parameter qm in method Service.getItems should be union type assignable QueryModel<KeyToEntityMap<E>>
Actual behavior:
Argument of type 'QueryModel<KeyToEntityMap<E>>' is not assignable to parameter of type 'QueryModel<{ x: string; }> & QueryModel<{ y: string; }>'.
Type 'QueryModel<KeyToEntityMap<E>>' is not assignable to type 'QueryModel<{ x: string; }>'.
Type '{ x: string; }' is not assignable to type 'KeyToEntityMap<E>'.
Code
type Key = "a" | "b";
type KeyToEntityMap<E extends Key> =
E extends "a" ? { x: string; } :
E extends "b" ? { y: string; } :
never;
class QueryModel<T>
{
filters: [keyof T, string][] = [];
}
class Service<E extends Key>
{
getItems(qm: QueryModel<KeyToEntityMap<E>>): KeyToEntityMap<E>[]
{
return [];
}
}
type IServiceFactory = { [E in Key]: Service<E> };
class ServicesFactory implements IServiceFactory
{
a: Service<"a"> = new Service<"a">();
b: Service<"b"> = new Service<"b">();
}
const sf: IServiceFactory = new ServicesFactory();
class Same<E extends Key>
{
fail(e: E)
{
let queryModel = new QueryModel<KeyToEntityMap<E>>();
//Here getItems parameter gets wrong intersection type QueryModel<{ x: string; }> & QueryModel<{ y: string; }>
let items: KeyToEntityMap<E> = sf[e].getItems(queryModel);
}
}
Output
"use strict";
class QueryModel {
constructor() {
this.filters = [];
}
}
class Service {
getItems(qm) {
return [];
}
}
class ServicesFactory {
constructor() {
this.a = new Service();
this.b = new Service();
}
}
const sf = new ServicesFactory();
class Same {
fail(e) {
let items = sf[e].getItems(new QueryModel());
}
}
Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"useDefineForClassFields": false,
"alwaysStrict": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"downlevelIteration": false,
"noEmitHelpers": false,
"noLib": false,
"noStrictGenericChecks": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"esModuleInterop": true,
"preserveConstEnums": false,
"removeComments": false,
"skipLibCheck": false,
"checkJs": false,
"allowJs": false,
"declaration": true,
"experimentalDecorators": false,
"emitDecoratorMetadata": false,
"target": "ES2017",
"module": "ESNext"
}
}
Playground Link: Provided
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 liên kết Playground được cung cấp và bản tái hiện TypeScript tối thiểu dưới các tùy chọn trình biên dịch đã nêu. Theo dõi cách tra cứu Service được lập chỉ mục và kiểu điều kiện generic được kiểm tra, sau đó thêm một trường hợp hồi quy cho lỗi assignability đã được báo cáo. Hoàn thành có nghĩa là ví dụ được kiểm tra kiểu mà không xuất hiện chẩn đoán sai về kiểu intersection, đồng thời vẫn giữ nguyên kiểu trả về được minh họa.
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
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- 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