-
-
Notifications
You must be signed in to change notification settings - Fork 502
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
Changes from 1 commit
72d6c6f
97d3dc8
886c4fb
87307fd
b4c76e6
8ddbc11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,3 +143,69 @@ import type {Exact} from '../index'; | |
fn(input); | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
{ // Spec - union of array | ||
type Type = Array<{x: string}> & Array<{z: number}>; | ||
const fn = <T extends Exact<Type, T>>(args: T) => args; | ||
|
||
{ // It should accept valid input | ||
const input = [{ | ||
x: '', | ||
z: 1, | ||
}]; | ||
fn(input); | ||
} | ||
|
||
{ // It should reject missing field | ||
const input = [{ | ||
z: 1, | ||
}]; | ||
// @ts-expect-error | ||
fn(input); | ||
} | ||
|
||
{ // It should reject missing field | ||
const input = [{ | ||
x: '', | ||
}]; | ||
// @ts-expect-error | ||
fn(input); | ||
} | ||
|
||
{ // It should reject incorrect type | ||
const input = [{ | ||
x: 1, | ||
z: 1, | ||
}]; | ||
// @ts-expect-error | ||
fn(input); | ||
} | ||
|
||
{ // It should reject excess field | ||
const input = [{ | ||
x: '', | ||
y: '', | ||
z: 1, | ||
}]; | ||
// @ts-expect-error | ||
fn(input); | ||
} | ||
} | ||
|
||
{ // 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 commentThe 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 👍 |
||
const fn = <T extends Exact<Type, T>>(args: T) => args; | ||
|
||
{ // It should reject excess field | ||
const input = [{ | ||
x: '', | ||
z: 1, | ||
d: { | ||
e: 'test', | ||
f: true, // Excess field | ||
}, | ||
}]; | ||
// @ts-expect-error | ||
fn(input); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} |
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.
👍