microsoft / microsoft/TypeScript

Branded literal type widened in unexpected way

Đang mở
#61,093 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Domain: Intersection Help Wanted
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

literal intersection branded string

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about branded strings/literal intersections.

### ⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.3#code/CYUwxgNghgTiAEYD2A7AzgF3hgngBxAGkQcAueAVxQEsBHChNHAWwCMkIBuAKG9wPgAVfCACSAEQA8wgQF5KKANYokAdxQAaeBPggAHhhApgaeJhjUUAc3jzzlqwD5b28fABk8AN7wA2vyISAF0AfnIZBABfHm4AMyowDGpUbBEJaREtHX1DY1N7a0cACmpgcgkASnC0qQis8Wcvbnh4OAwKGBR4Up5I3gD4YhwAeVjpbIMjEyEa53lBCdzpiPSoFBwtS1iQGFdnENd4chQQADcdmIGI0fG3HKnTFYaXBbvJvJmCdK2dz5B9v5HeAnc4wS4ieAABRgSFOpRAaEkkN072WsxcPjgUGAqAgOD8gm6XSGN0hjiC1QIpOc0V4AHo6fAALQssAUDAspm8SyGGCxKBgBAAMSQSG8zRa8FioqKVXgpyQPW4fW4yHQWBFYvkAQkRQARJq9RUzFAkmhYtQEX90prHDwGZL4AA9EK8NWYeAAIVg5GhsPhiICSFi8FtGIlLV8mop4sdjulSFl3j6jr60SAA

### 💻 Code

```ts
declare const typeKey: unique symbol;

type TypeID = ID & { [typeKey]?: Type };

function typeID(id: ID): TypeID {
return id;
}

type KeyOf = TID extends TypeID ? ID : never;

type TypeOf = TID extends TypeID ? Type : never;

type Provides

= { readonly [T in KeyOf

]: TypeOf

};

// ---cut---

interface Foo {
foo(): void;
}

const Foo = typeID("Foo") satisfies TypeID;
// ^? const Foo: TypeID

const Bar: Provides = {
[Foo]: {
foo() {}
}
};
```

### 🙁 Actual behavior

Fails to compile with error:

```
Property 'Foo' is missing in type '{ [x: string]: { foo(): void; }; }' but required in type 'Provides>'.
```

### 🙂 Expected behavior

I'd expect it to compile. Rather than widening the literal type to `string`, I'd expect it to discard the `{ [typeKey]?: Type }` part, since that's pretty much bogus at runtime anyway.

However, it should also not lose the type information for the generic types. For instance, if I change the declaration to...

```ts
type TypeID = ID;
```

...then it does compile, but so does this...

```ts
const Bar: Provides = {
[Foo]: {
// No error about missing member `foo` here!
}
};
```

### Additional information about the issue

Possibly related to https://github.com/microsoft/TypeScript/issues/43852#issuecomment-1671115898

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với bản tái hiện trong TypeScript Playground và so sánh hành vi của branded literal với phần thảo luận liên quan trong issue #43852. Truy vết đường đi của quá trình kiểm tra kiểu cho các định nghĩa `TypeID`, `KeyOf`, `TypeOf` và `Provides`; được xem là hoàn tất khi ví dụ biên dịch được nhưng vẫn báo cáo thành viên `foo` bị thiếu khi thích hợp.

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, devtools
Loại issue
Lỗi
Độ 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

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.