microsoft / microsoft/TypeScript

Generic constraint generates a TS2344

Aperta
#36,852 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

TypeScript Version: 3.7.x, 3.8.1, 3.9.x (next)

Search Terms: Generic constraint 2344

Code

type KeyOf<T> = keyof ((T extends any ? (k: T)=>void : never) extends ((k: infer I)=>void) ? I : never);

type Constraint<T> = KeyOf<T> extends never
  ? any
    // : KeyOf<any> extends KeyOf<T>
    // ? any
  : (
    'a' extends KeyOf<T>
      ? never
      : any
    );

type ForceConstraint<T> = T extends Constraint<T> ? T : never;

interface Interface<T extends Constraint<T>> {
  b: string;
}

type Infer<CB> = CB extends () => infer TReturn
  ? ForceConstraint<TReturn>
  : never;

type InferWithConstraint<CB> = ForceConstraint<Infer<CB>>;

type InferInterfaceFromCallback<CB> = Interface<InferWithConstraint<CB>>; // invalid => NOT OK
type InferInterfaceFromCallback1<CB> = Interface<Infer<CB>;> // invalid too => NOT OK

// just debug purpose
type A1 = Interface<unknown>;
type A2 = Interface<any>; // invalid => OK
type A3 = Interface<never>;
type A4 = Interface<undefined>;
type A5 = Interface<number>;
type A6 = Interface<string>;
type A7 = Interface<null>;
type A8 = Interface<object>;
type A9 = Interface<{}>;
type A_FAIL1 = Interface<{ a: 'b' }>; // invalid => OK
type A_FAIL2 = Interface<{ a: 'b' } | 1>; // invalid => OK

Playground Link

Expected behavior:

type InferInterfaceFromCallback<CB> = Interface<InferWithConstraint<CB>>;

Should be valid

Explanation:

So, i was trying to apply a specific constraint on a generic type.

Constraint<T> 'returns' never if T contains a property called a, else it returns any

ForceConstraint<T> tries to help typescript to know if T is valid, else it returns never

Interface<T extends Constraint<T>> forces its generic type to respect Constraint

Infer<CB> infers the return type of CB and ensures it follows Constraint

InferWithConstraint<CB> forces again Infer<CB> to be compliant with Constraint (even if it is already)

So Interface<InferWithConstraint<CB>> should be valid, sadly, typescript reports:

TS2344: Type 'ForceConstraint<Infer<CB>>' does not satisfy the constraint 'Constraint<ForceConstraint<Infer<CB>>>'.

Potentially related issues:

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il Playground Link fornito e riproduci la diagnostica TS2344 usando l’esempio minimo di tipo generico. Confronta la validità prevista di Interface<InferWithConstraint> con l’errore di vincolo segnalato e consulta le Issues correlate 31736 e 34604. Il lavoro è completato quando l’alias di tipo valido non produce più TS2344, mentre gli esempi non validi continuano a essere rifiutati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.