microsoft / microsoft/TypeScript

Interface that extends another no longer constrains types like the original

Đang mở
#60,008 1 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: check: Type Inference 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

interface
interface constrain
extend interface

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about interfaces.
⏯ Playground Link

https://www.typescriptlang.org/play/?#code/PQKhAIGUFEBUFUAK4C8b0c1z4TAFABmArgHYDGALgJYD2p4lAngA4CmAPLAHwAUAlAC5wscAG9wAJzaVikhqWIAbJeACGAZ3BkA1qVoB3BppEBucAF98zduGgA3NqUoBZNS1TgASm3K1JACYcGpSS1KQA5gA0do7O3KbWrGzgADLUIU5sklwOTpTgbAAelE4BWnnxnry8bPaUwrCVlPyo3OD2tNQBrQA+4uAAFmqkAUpszby0AEYAVr4NIs1CHV0Blon4xSz+BeGlkoRq5CmwyQHN0AC21JQHuXGu7oUlZRWPbiztYvjgf+oBC6PdKZUjZB75M62YqlUZaHRsJi0QhLD7uPi-f5YmxsRrNKFsKKYrF-JQZWHZYQgik5JpolgAbTpkOSAF1uESSf9aCwaPQNAB+YQAQUBzWpWUkAHleXRSFp+tNaLRxiNOf8Vp1uoksdIrrRHOLyZKIc4CS9YeVwAikSjmc5PhiuX8cXjHgT1VyyaDKWljWDac1Pkz8WyOcSSTy+fKhbF8hKAzLowrwEqVWw1RHNWtElZQBBoAA5AAiUDgSFQ2Cr1ZQuAI+HzvwggFByKAjW5MRhsEJNgg4rjnS43O7giSEZXCZqWbh8VoaNQ0DSEajdkSDx7XW73McTuPOafmYDAcDN-AN48AAUoGgAtNsFnfJJJ-El2AP2ED8puRzkJNM1JIk6PNOs7gPOi7LquBKfs437buA460EB+QHuAR4nmeja4Ce4DCmSJjIowgwpP41AROEaiqDivavikADCypKEOW7gvaTweDCbx7ux7S1tBzE-qaPGbP2DEqgJ8GIch+4WDOAhgQuGSQVo-EbsOkm7lOsmHsep4Rs6BmGUZxkmaZJnoVet73lQj7PpItEcGJTFqSxv6pgB0kFLJoHgUpK4qeuX7qaOCGacB2lobpmFgE24CADLk4AAJLONkRwnERC5gdQVwsEonacXCREkWE5GkJRjDJDR+ypcc9GMclBxpRMLmCWxnwWlxQboh1hWqUFrlCY6AxWA5TkNTVJwSSFUncSB8m+Uu-lrh+U1uTNWkJJFGH6WZu17ftB1GRZ153kU7A2dkdmjfVKWHLVq0cH+Hmzd582KYtUGBbBwVrWFKERehcXRSAsUJeNd3pZQwwFAVVpQ8VZEUVRyTqKMAJWiExCEIQVW3U14BjXjtWIEoxAaMWGQsAu5CDA9bXPLD7z5ENjPLWwMGUHBrFdV84gRgElPU7TjxCearM2oR9NfLU9husz7ghu6bLZtq+AjaJN2NcTpPk4LlA0w9O5IS9clzu9ylsxzXO-cbG06dth2O07zsu38x1WWdD6XS+GsquDTUk2TFMaFT+vC-1glPYBJs+ebS19d9A1G55qGA8DsWtgxLCdlTIThBERXgH4AQpOEmRqOshGw-nhfVRDKQGP4OgaLjWvpVnTCIJopRWz9g0M68hU898EYVxzCbc0r0KD1aEt2sPvA7S6yRy2aySeiS3o0lS-qT-LjJsQS7Ib1iUZyoKIpisCu-SrK-LgIqjEZqQG8qwEOr-HqBrNfGN+iyj4tESSwXkvCq7BV6UA9KArekod4+kDPSRWLJ2DH1AWffksYjTwKTOfB+qYn6ZixG-D+fwBYhyFs0f+09LTwiAfPekfA6gQODIfZWwgtTvzVmeDW2cu6ZF7knUKttwqmwUhBeOX1OZ92Tibe2elXYKMUftd2p1zqUFsj7ZIjkeSd27uzQ27lo4bVjuIz6K0WoaWEf9Ta6FmxAA

💻 Code
/** SETUP ========================== */
function type<T>(): T { return null as unknown as T; }
type EventMap = Record<string, Event>;
type Listener<TEvent extends Event> = ((evt: TEvent) => void) | { handleEvent(object: TEvent): void };

export interface TypedEventEmitter<TEventMap extends EventMap> {
    addEventListener<TEventType extends keyof TEventMap>(
        type: TEventType,
        listener: Listener<TEventMap[TEventType]>,
        options?: AddEventListenerOptions | boolean,
    ): void;
    removeEventListener<TEventType extends keyof TEventMap>(
        type: TEventType,
        listener: Listener<TEventMap[TEventType]>,
        options?: EventListenerOptions | boolean,
    ): void;
}
/** END SETUP ========================== */

/**
 * ✅ Sanity test
 */
type<TypedEventEmitter<{ foo: Event }>>() satisfies TypedEventEmitter<{ foo: Event }>; // ✅

// @ts-expect-error
type<TypedEventEmitter<{ bar: Event }>>() satisfies TypedEventEmitter<{ foo: Event }>; // ✅

/**
 * ✅ Alias of the original type
 */
type CoolEventEmitter<TEventMap extends EventMap> = TypedEventEmitter<TEventMap>;

type<CoolEventEmitter<{ foo: Event }>>() satisfies TypedEventEmitter<{ foo: Event }>; // ✅

                                                                                      // @ts-expect-error
type<CoolEventEmitter<{ bar: Event }>>() satisfies TypedEventEmitter<{ foo: Event }>; // ✅

/**
 * ❌ Interface that simply extends the original type
 */
interface CoolInterfaceEventEmitter<TEventMap extends EventMap> extends TypedEventEmitter<TEventMap> { }

type<CoolInterfaceEventEmitter<{ foo: Event }>>() satisfies TypedEventEmitter<{ foo: Event }>; // ✅

                                                                                               // @ts-expect-error
type<CoolInterfaceEventEmitter<{ bar: Event }>>() satisfies TypedEventEmitter<{ foo: Event }>; // ❌

/**
 * ❌ Interface that extends the original type and adds stuff
 */
interface CoolInterfacePlusDispatchEventEmitter<TEventMap extends EventMap> extends TypedEventEmitter<TEventMap> {
    dispatchEvent<TEventType extends keyof TEventMap>(ev: TEventMap[TEventType]): void;
}

type<CoolInterfacePlusDispatchEventEmitter<{ foo: Event }>>() satisfies TypedEventEmitter<{ foo: Event }>; // ✅

                                                                                                           // @ts-expect-error
type<CoolInterfacePlusDispatchEventEmitter<{ bar: Event }>>() satisfies TypedEventEmitter<{ foo: Event }>; // ❌

/**
 * ✅ Copy pasting the code instead of extending the interface works
 */
interface CopyPastedEventEmitter<TEventMap extends EventMap> {
    addEventListener<TEventType extends keyof TEventMap>(
        type: TEventType,
        listener: Listener<TEventMap[TEventType]>,
        options?: AddEventListenerOptions | boolean,
    ): void;
    removeEventListener<TEventType extends keyof TEventMap>(
        type: TEventType,
        listener: Listener<TEventMap[TEventType]>,
        options?: EventListenerOptions | boolean,
    ): void;
    dispatchEvent<TEventType extends keyof TEventMap>(ev: TEventMap[TEventType]): void;
}

type<CopyPastedEventEmitter<{ foo: Event }>>() satisfies TypedEventEmitter<{ foo: Event }>; // ✅

                                                                                            // @ts-expect-error
type<CopyPastedEventEmitter<{ bar: Event }>>() satisfies TypedEventEmitter<{ foo: Event }>; // ✅
🙁 Actual behavior

As soon as the original interface is extended – whether the extender adds properties, does not add properties, uses the type parameter, or doesn't use the type parameter – I seem to lose the ability to constrain types in the same way as the original interface can.

🙂 Expected behavior

I would expect an interface that extends another to constrain types in exactly the same way as the original.

Additional information about the issue

No response

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

Tái hiện hành vi bằng TypeScript Playground được liên kết và mã trong issue, so sánh interface ban đầu, type alias và các interface mở rộng. Theo dõi đường dẫn kiểm tra kiểu cho các ràng buộc generic và thêm một regression test; được xem là hoàn thành khi một interface mở rộng giữ nguyên ràng buộc ban đầu và các trường hợp @ts-expect-error hiện có hoạt động như mong đợi.

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
38/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.