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

Merged
merged 2 commits into from
Feb 19, 2025

Conversation

ben-tilden
Copy link
Contributor

@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"

@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?

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"
```
@ben-tilden ben-tilden force-pushed the fix-extracted-value-type branch from b475416 to 12ea051 Compare February 19, 2025 01:55
@ben-tilden ben-tilden force-pushed the fix-extracted-value-type branch from 12ea051 to 5c4f05a Compare February 19, 2025 02:13
@ben-tilden
Copy link
Contributor Author

Apologies for the delay - added!

Copy link
Member

@fb55 fb55 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

@fb55 fb55 merged commit 6e31241 into cheeriojs:main Feb 19, 2025
9 checks passed
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