-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
72d6c6f
fix(Exact): support array union
zorji 97d3dc8
fix(Exact): support readonly array
zorji 886c4fb
fix(Exact): format
zorji 87307fd
Update source/exact.d.ts
zorji b4c76e6
fix(Exact): format
zorji 8ddbc11
Update exact.d.ts
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,44 @@ import type {Exact} from '../index'; | |
fn(input); | ||
} | ||
|
||
{ // It should reject readonly array | ||
const input = [{code: ''}] as ReadonlyArray<{code: string}>; | ||
// @ts-expect-error | ||
fn(input); | ||
} | ||
|
||
{ // It should accept array with optional property | ||
const input = [{code: '', name: ''}]; | ||
fn(input); | ||
} | ||
|
||
{ // It should reject array with excess property | ||
const input = [{code: '', name: '', excessProperty: ''}]; | ||
// @ts-expect-error | ||
fn(input); | ||
} | ||
|
||
{ // It should reject invalid type | ||
const input = ''; | ||
// @ts-expect-error | ||
fn(input); | ||
} | ||
} | ||
|
||
{ // Spec - readonly array | ||
type Type = ReadonlyArray<{code: string; name?: string}>; | ||
const fn = <T extends Exact<Type, T>>(args: T) => args; | ||
|
||
{ // It should accept array with required property only | ||
const input = [{code: ''}]; | ||
fn(input); | ||
} | ||
|
||
{ // It should accept readonly array | ||
const input = [{code: ''}] as ReadonlyArray<{code: string}>; | ||
fn(input); | ||
} | ||
|
||
{ // It should accept array with optional property | ||
const input = [{code: '', name: ''}]; | ||
fn(input); | ||
|
@@ -143,3 +181,156 @@ import type {Exact} from '../index'; | |
fn(input); | ||
} | ||
} | ||
|
||
{ // 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 readonly array + non readonly array | ||
type Type = ReadonlyArray<{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; f: boolean}}>; | ||
const fn = <T extends Exact<Type, T>>(args: T) => args; | ||
|
||
{ // It should accept valid input | ||
const input = [{ | ||
x: '', | ||
z: 1, | ||
d: { | ||
e: 'test', | ||
f: true, | ||
}, | ||
}]; | ||
fn(input); | ||
} | ||
|
||
{ // It should reject excess field | ||
const input = [{ | ||
x: '', | ||
z: 1, | ||
d: { | ||
e: 'test', | ||
f: true, | ||
g: '', // Excess field | ||
}, | ||
}]; | ||
// @ts-expect-error | ||
fn(input); | ||
} | ||
|
||
{ // It should reject missing field | ||
const input = [{ | ||
x: '', | ||
z: 1, | ||
d: { | ||
e: 'test', | ||
// Missing f: boolean | ||
}, | ||
}]; | ||
// @ts-expect-error | ||
fn(input); | ||
} | ||
|
||
{ // It should reject missing field | ||
const input = [{ | ||
x: '', | ||
z: 1, | ||
d: { | ||
e: 'test', | ||
f: '', // Type mismatch | ||
}, | ||
}]; | ||
// @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 |
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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