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(Exact): support array union #421
Conversation
@JasonShin could you help review this PR? including grammar issues lol |
test-d/exact.ts
Outdated
} | ||
|
||
{ // Spec - union of array with nested fields | ||
type Type = Array<{x: string}> & Array<{z: number; d: {e: string}}>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @zorji !! I was testing this again and found that union of 2 ReadOnlyArray breaks the type. But I think creating a union of 2 read-only arrays is already broken in TS...
Unless you think this is also important to handle in this PR 👍
}]; | ||
// @ts-expect-error | ||
fn(input); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly we can include more test coverages to check excess, exact, missing and mismatching fields for the union array test
source/exact.d.ts
Outdated
*/ | ||
: {[Key in keyof ParameterType]: Exact<ParameterType[Key], InputType[Key]>} & Record<Exclude<keyof InputType, KeysOfUnion<ParameterType>>, never>; | ||
export type Exact<ParameterType, InputType> = | ||
// Converting union of array to array of union. i.e. A[] & B[] => (A & B)[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise looks good, I will leave grammar and spelling checks to others 😊 |
@@ -143,3 +143,69 @@ import type {Exact} from '../index'; | |||
fn(input); | |||
} | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A little bit out of the scope of this PR but can we also include exhaustive tests against ReadonlyArray as well? I just want to ensure ReadonlyArray will work consistently in the future along with the regular mutable arrays.
We can just copy & paste Array spec for the ReadonlyArray test. I just quickly tested Array spec against ReadonlyArray and it seems to work fine
@JasonShin looks like ReadOnly is an easy fix but I am not sure whether this is a breaking change. I'll put them in 2 separate PRs. https://github.com/zorji/type-fest/pull/1/files |
@JasonShin actually I think I found a way |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good from my end, thanks for the updates!
@sindresorhus Please review this thanks 👍 |
source/exact.d.ts
Outdated
Return `never` if T is not an array. | ||
It creates a type-safe way to access the element type of `unknown` type. | ||
*/ | ||
type ArrayElement<T> = T extends readonly unknown[]? T[0] : never; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this use the trick mentioned in #118 (comment) ?
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
@sindresorhus I have tested and found the suggested implementation doesn't work for union of array |
Fixes #419