microsoft / microsoft/TypeScript

Strange intersection/variance behavior

Ouverte
#40,247 0 commentaires 0 réactions 1 personne assignée Voir sur GitHub

@weswigham y travaille déjà.

Depuis le 25/8/2020.

Needs Investigation
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

TypeScript Version: Nightly (d6dda230bce0828144e5f17539d6e3f9bf4c4f3b)

Search Terms: variance, intersection

Code

interface BaseEvent {
    type: string;
}
interface DerivedEvent extends BaseEvent {
    type: "mytype";
    payload: number;
};
type TransitionConfig<TEvent extends BaseEvent> = (event: TEvent) => void;
type TransitionsConfigMap<TEvent extends BaseEvent> =
    & { splendiferous?: never } // Un/comment this line
    & { [K in TEvent['type']]?: TransitionConfig<TEvent>; };
type TransitionsConfigArray<TEvent extends BaseEvent> = {
    [K in TEvent['type']]: TransitionConfig<TEvent extends { type: K; } ? TEvent : never>;
}[TEvent['type']];
type AssignAction<TEvent extends BaseEvent> = (meta: TransitionsConfigMap<TEvent> | TransitionsConfigArray<TEvent>) => void;
declare const d1: AssignAction<DerivedEvent>;
declare const b1: AssignAction<BaseEvent>;
const d2: typeof d1 = b1; // Sometimes an error
const b2: typeof b1 = d1; // Sometimes an error

As-is, there are errors on both d2 and b2. If you comment out the "splendiferous" line, the error on b2 goes away. (If you disable strict function types, the error on d2 also goes away.) I find this strange, since that line does not reference TEvent.

The type names give breadcrumbs back into xstate, but I've inlined and deleted so many things that the structure is substantially different.

Playground Link: https://www.typescriptlang.org/play?ts=4.0.2#code/JYOwLgpgTgZghgYwgAgEJwM4QKIDcLjIDeAUMucmAJ4AOEAXMhmFKAOYDcJAviaJLEQoAItGD4AJngJhkEAB6QQEjGkw58hUhUq0GyAEQBbKtToGuOmnCoAbAPZwJjEAFcjAI2hduXMygAVKDgQDGAwYHsQAGEomGA2AB4A6UIFJRU1LFSwAD5kAF5kAAoITTBGFPKASkL83HtgCT89ZCCQsIiojFiQeLYAWTgaZJy5RQJM9Gzy-IKyCgAyYiYaW0ngGGh7VwwAfhcy6GReHWWiZABtAGlkUDacy4ByfyeAXTeDtuDQ8MiYuIJUazDgnFp0b4dP7dXr9ACCUGCVGBMnGGVU0w0MjmxAW5BudxAD3Kz1eH0qP06-1hQKqqPSk1UF38jGuoO4yD2xNRh3wUFyPkudPApL07ze4JQcIwYTYIDhCC6IBRaQmygx6hyOOKRggYDgFKhSp6gMGwxVeWQAB9Ib9jTS2AikRbcrUCvVGs0SBIIAhbHAoCgEN1ZBIAIyMaWy+WK-6JUSsSRarg+v0BoMh5AeCPIKMJGNKxKY5MkYOhUMAJkY-nsMGQ4cKWbDXDLzCzVd0dFrTcb4Y4QA

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.