microsoft / microsoft/TypeScript
Conditional (using extends) parametrised type inference seems to be inconsistent
Open
@ahejlsberg is already working on this.
Since Aug 30, 2024.
Needs Investigation
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 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
💻 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.