microsoft / microsoft/TypeScript

`extends any ?` may return right hand in conditional types

Aperta
#41,349 2 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
2g 4h
PR unite (30g)
132

Descrizione

TypeScript Version: 4.0.5

Search Terms:

  • type inference
  • extends any
  • conditional types
  • right hand

Expected behavior:

type D1 = X1 <O1> returns false.

Actual behavior:

In the code below, type D1 = X1<O1> seems to return false, but it actually returns true.
If you change the seemingly irrelevant right-hand type in extends any ? written for union distribution to undefined, it will behave as intended.

Related Issues:

Code

type X1<T> = IfEq<
  AllKeys1<T>,
  keyof T,
  true,
  false
>;

type X2<T> = IfEq<
  AllKeys2<T>,
  keyof T,
  true,
  false
>;

type IfEq<X, Y, T, E> = [X] extends [Y] ? ([Y] extends [X] ? T : E) : E;

// AllKeys<{ x: ... } | { x: ...; y: ... }> -> "x" | ("x" | "y") -> "x" | "y"
type AllKeys1<O extends Object> = O extends any ? keyof O : never;
type AllKeys2<O extends Object> = O extends any ? keyof O : undefined;

type O1 = { x: string; y: number } | { x: boolean; z: number }
type A1 = AllKeys1<O1> // "x" | "y" | "z"
type A2 = AllKeys2<O1> // "x" | "y" | "z"
type B = keyof O1 // "x"

type C1 = IfEq<A1, B, true, false> // false
type C2 = IfEq<A2, B, true, false> // false
type D1 = X1<O1> // true ???
type D2 = X2<O1> // false

type O2 = { x: string; y: number } | { x: boolean; y: Object }
type E1 = X1<O2> // true
type E2 = X2<O2> // true
Output
"use strict";

Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

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 la riproduzione fornita in Playground usando TypeScript 4.0.5 e confronta i risultati dei tipi condizionali per D1, D2, E1 ed E2. Segui la gestione dei tipi condizionali distributivi da parte del compilatore e verifica che i risultati attesi ed effettivi riportati vengano risolti in modo coerente senza modificare gli altri casi.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.