Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): fix ExtractedValue type #4334

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ben-tilden
Copy link

@ben-tilden ben-tilden commented Jan 4, 2025

Noticed this while working with extract

TypeScript does not automatically distribute conditionals unless the generic type itself represents a union. So, in ExtractedValue, V['value'] is not distributed unless inferred as a separate generic type.

EDIT: Adding issue link

// Testing distributed conditional over an indexed access type
interface Obj {
  value: string | number;
}

type TestIndexedObj<V extends { value: unknown }> = V['value'] extends number ? 'extends number' : 'does not extend number';
type TestIndexedObjResult = TestIndexedObj<Obj> // "does not extend number"

// Testing distributed conditional over a union
type ObjValue = Obj['value'];

type TestUnion<V> = V extends number ? 'extends number' : 'does not extend number';
type TestUnionResult = TestUnion<ObjValue> // "does not extend number" | "extends number"

// Testing distributed conditional over an inferred generic from an indexed access type
type TestInferredIndexedObj<V extends { value: unknown }> = V['value'] extends infer U ? (U extends number ? 'extends number' : 'does not extend number') : never;
type TestInferredIndexedObjResult = TestInferredIndexedObj<Obj> // "does not extend number" | "extends number"

TypeScript does not automatically distribute conditionals unless the generic type itself represents a union. So, in `ExtractedValue`, `V['value']` is not distributed unless inferred as a separate generic type.

```
// Testing distributed conditional over an indexed access type
interface Obj {
  value: string | number;
}

type TestIndexedObj<V extends { value: unknown }> = V['value'] extends number ? 'extends number' : 'does not extend number';
type TestIndexedObjResult = TestIndexedObj<Obj> // "does not extend number"

// Testing distributed conditional over a union
type ObjValue = Obj['value'];

type TestUnion<V> = V extends number ? 'extends number' : 'does not extend number';
type TestUnionResult = TestUnion<ObjValue> // "does not extend number" | "extends number"

// Testing distributed conditional over an inferred generic from an indexed access type
type TestInferredIndexedObj<V extends { value: unknown }> = V['value'] extends infer U ? (U extends number ? 'extends number' : 'does not extend number') : never;
type TestInferredIndexedObjResult = TestInferredIndexedObj<Obj> // "does not extend number" | "extends number"
```
@fb55
Copy link
Member

fb55 commented Jan 5, 2025

Thanks for this awesome PR! Could you add a types test to validate this works (and keeps working), using the same expectTypeOf already used for a bunch of type checks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants