-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: β add union support for Mark* methods (#324)
* test: π§ͺ move tests for MarkOptional to separate file * test: β add UnionExample to all Mark* methods * feat: β add union support for Mark* methods * docs: π changeset
- Loading branch information
Showing
7 changed files
with
100 additions
and
25 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"ts-essentials": patch | ||
--- | ||
|
||
Add union support to all Mark\* methods: `MarkRequired`, `MarkOptional`, `MarkReadonly` and `MarkWritable` |
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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { AssertTrue as Assert, IsExact } from "conditional-type-checks"; | ||
import { MarkOptional, OptionalKeys, RequiredKeys, WritableKeys } from "../lib"; | ||
|
||
function testMarkOptional() { | ||
type Example = { | ||
required1: number; | ||
required2: string; | ||
optional1?: null; | ||
optional2?: boolean; | ||
}; | ||
|
||
type UnionExample = MarkOptional< | ||
Pick<Example, "required1" | "optional1"> | Pick<Example, "required2" | "optional1">, | ||
"optional1" | ||
>; | ||
|
||
let unionElementFields: UnionExample = { | ||
required1: 1, | ||
optional1: null, | ||
}; | ||
|
||
unionElementFields = { | ||
required2: "2", | ||
optional1: null, | ||
}; | ||
|
||
type cases = [ | ||
Assert<IsExact<MarkOptional<Example, never>, Example>>, | ||
Assert<IsExact<MarkOptional<Example, OptionalKeys<Example>>, Example>>, | ||
Assert<IsExact<MarkOptional<Example, RequiredKeys<Example>>, Partial<Example>>>, | ||
Assert< | ||
IsExact< | ||
MarkOptional<Example, "required1">, | ||
{ | ||
required1?: number; | ||
required2: string; | ||
optional1?: null; | ||
optional2?: boolean; | ||
} | ||
> | ||
>, | ||
// @ts-expect-error do NOT support union types | ||
MarkOptional<Example | { a: 1 }, "required1">, | ||
]; | ||
} |
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
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