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(Exact): support array union #421

Merged
merged 6 commits into from Aug 22, 2022

Conversation

zorji
Copy link
Contributor

@zorji zorji commented Jul 9, 2022

Fixes #419

@zorji zorji marked this pull request as ready for review July 9, 2022 08:36
@zorji
Copy link
Contributor Author

zorji commented Jul 9, 2022

@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}}>;

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...

Screen Shot 2022-07-11 at 9 09 11 am

Unless you think this is also important to handle in this PR 👍

}];
// @ts-expect-error
fn(input);
}
Copy link

@JasonShin JasonShin Jul 10, 2022

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

*/
: {[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)[]

Choose a reason for hiding this comment

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

👍
Screen Shot 2022-07-11 at 9 13 55 am

@JasonShin
Copy link

Otherwise looks good, I will leave grammar and spelling checks to others 😊

@@ -143,3 +143,69 @@ import type {Exact} from '../index';
fn(input);
}
}

Copy link

@JasonShin JasonShin Jul 10, 2022

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.

Screen Shot 2022-07-11 at 9 22 56 am

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

@zorji
Copy link
Contributor Author

zorji commented Jul 11, 2022

@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

@zorji
Copy link
Contributor Author

zorji commented Jul 11, 2022

@JasonShin actually I think I found a way

Copy link

@JasonShin JasonShin left a 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!

@JasonShin
Copy link

@sindresorhus Please review this thanks 👍

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;
Copy link
Owner

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

source/exact.d.ts Outdated Show resolved Hide resolved
zorji and others added 3 commits August 22, 2022 21:37
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
@zorji
Copy link
Contributor Author

zorji commented Aug 22, 2022

Could this use the trick mentioned in #118 (comment) ?

@sindresorhus I have tested and found the suggested implementation doesn't work for union of array

@sindresorhus sindresorhus merged commit edcad04 into sindresorhus:main Aug 22, 2022
@zorji zorji deleted the fix/exact-array branch August 23, 2022 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exact intersection type support
3 participants