microsoft / microsoft/TypeScript

Conditional (using extends) parametrised type inference seems to be inconsistent

Aperta
#59,700 2 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@ahejlsberg ci sta già lavorando.

Dal 30/8/2024.

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

Descrizione

🔎 Search Terms

repo:microsoft/inconsistent typing with extends and parametrised types
repo:microsoft/TypeScript inconsistent typing with extends
repo:microsoft/TypeScript inconsistent conditional typing

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

https://www.typescriptlang.org/play/?#code/MYGwhgzhAEDCIFcB2BrAPAOWgUwB4BdskATGCfAJwEskBzAPmgG8BfAKFEhgGUAHbYFTAh4ydFjyESZSjQY4CRUnESpMjJm2jQI-QcIBiVbCGLQAvNADkAMzEBPG8dNWA3G3Zt89-tD4ChEVUUAFFFaVFUCz89QMj0MCR7RkklGHi0ROToAH5oSgRsaAAuaBthCGxXaAB6GvyKQoA6Lx8i+LCpUn99ILFojKyU8OUeuODMpMY8gqLS8pBK6rqG5rY2PF4Aewp8fLa-LYBbbAyAFQUumCskHaPhK2gAH2tdAIeAGmgJEZlqOkYlguqWk1luFHuICsWlyKjE6hhpWBv1esQeMLyY2EGQw9ER0CQ2AAbtgKO5Wr4sSBOmkMHdhNErG9eo8QcobvSobDZiUyhUqrV6gtKi1vL46RDhDTpFTGeDIayUUy0VyZo05nzFgKVsLsC02CsAKqVaBbGz7XwAC1JRXwW2gACMisBjrwwPgqA6QEUAO5UfCW6C0IikqjAaBuihgI4QFqbHZ7MVFAAKAEY0MirmDOY8Xsr3lDAcwYaAxKVuMdThMzl9bFstlZ6O4WOSk9A0-KGZY02gOZLC8t6m2O5zokxoKXUKUMgAiGz1mdN6DsYep5mBaI9-MspcrVfrrvMCfBcsq2fzraL6org5pqnS0gSyGbtcqy5pdupzsgbnq3m6wdVmwCkU1TJ9hAAES2bAIDpfAHwgWVuy-Uc2RgNMDx-NVCn-flAN1EDP3vX5wJAOkfQMfkX17TDG3fUEt2-OjsI1ADBU1E0gA

💻 Code
class Clunk<N extends string> {}
class SpecialClunk<N extends string> extends Clunk<N> {
  specialField = 'funkyfield';
}

type SpecialClunkExtendsClunk = SpecialClunk<any> extends Clunk<any> ? true : false; // true.
type ClunkExtendsSpecialClunk = Clunk<any> extends SpecialClunk<any> ? true : false; // true.

export type SomeClunk<T extends 'normal' | 'special', N extends string> = T extends 'normal'
  ? Clunk<N>
  : T extends 'special'
  ? SpecialClunk<N>
  : never;

type SpecialExtendsNormal = 'special' extends 'normal' ? true : false; // false.
type NormalExtendsSpecial = 'normal' extends 'special' ? true : false; // false.

// Use of type here to be compatible with generic params.
export type P1<T extends 'normal' | 'special'> = {
  clunk: SomeClunk<T, 'foo'>;
};

type P1normal = P1<'normal'>; // type P1normal = { clunk: Clunk<"foo">; }
type P1special = P1<'special'>; // type P1special = { clunk: SpecialClunk<"foo">; }
type P1NormalDoesNotExtendsSpecial = P1normal extends P1special ? true : false; // false

type P1SpecialExtendsNormal = P1special extends P1normal ? true : false; // true

// Below is false, but expected to be true, like P1SpecialExtendsNormal *** BUG?
type P1SpecialExtendsNormalNowFalse = P1<'special'> extends P1<'normal'> ? true : false; // false 
🙁 Actual behavior

P1SpecialExtendsNormal is true
P1SpecialExtendsNormalNowFalse is false

🙂 Expected behavior

P1SpecialExtendsNormal is true
P1SpecialExtendsNormalNowFalse is true

Additional information about the issue

I can't tell if this might be related to https://github.com/microsoft/TypeScript/issues/44945

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.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.