Skip to content

Commit

Permalink
Fix: MarkRequired and MarkWritable types when Keys is any (#421)
Browse files Browse the repository at this point in the history
* fix: mark-required and mark-writable with any

* test: add any case for mark-optional and mark-readonly

* chore: add changeset
  • Loading branch information
som-sm authored Dec 26, 2024
1 parent d3b56d7 commit d02bf22
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-cycles-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ts-essentials": patch
---

Fix `MarkRequired<Type, Keys>` & `MarkWritable<Type, Keys>` types when `Keys` is `any`
2 changes: 1 addition & 1 deletion lib/mark-required/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Prettify } from "../prettify";

export type MarkRequired<Type, Keys extends keyof Type> = Type extends Type
? Prettify<Type & Required<Pick<Type, Keys>>>
? Prettify<Type & Required<Omit<Type, Exclude<keyof Type, Keys>>>>
: never;
2 changes: 1 addition & 1 deletion lib/mark-writable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { WritableKeys } from "../writable-keys";
import { Prettify } from "../prettify";

export type MarkWritable<Type, Keys extends keyof Type> = Type extends Type
? Prettify<Readonly<Type> & Writable<Pick<Type, (Type extends object ? WritableKeys<Type> : never) | Keys>>>
? Prettify<Readonly<Type> & Writable<Omit<Type, Exclude<keyof Type, WritableKeys<Type & object> | Keys>>>>
: never;
1 change: 1 addition & 0 deletions test/mark-optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Example = {
function testMarkOptional() {
type cases = [
Assert<IsExact<MarkOptional<Example, never>, Example>>,
Assert<IsExact<MarkOptional<Example, any>, Partial<Example>>>,
Assert<IsExact<MarkOptional<Example, OptionalKeys<Example>>, Example>>,
Assert<IsExact<MarkOptional<Example, RequiredKeys<Example>>, Partial<Example>>>,
Assert<
Expand Down
1 change: 1 addition & 0 deletions test/mark-readonly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function testMarkReadonly() {

type cases = [
Assert<IsExact<MarkReadonly<Example, never>, Example>>,
Assert<IsExact<MarkReadonly<Example, any>, Readonly<Example>>>,
Assert<IsExact<MarkReadonly<Example, ReadonlyKeys<Example>>, Example>>,
Assert<IsExact<MarkReadonly<Example, WritableKeys<Example>>, Readonly<Example>>>,
Assert<IsExact<ReadonlyKeys<ExampleWithReadonlyRequired1>, "readonly1" | "readonly2" | "required1">>,
Expand Down
1 change: 1 addition & 0 deletions test/mark-required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Example = {
function testMarkRequired() {
type cases = [
Assert<IsExact<MarkRequired<Example, never>, Example>>,
Assert<IsExact<MarkRequired<Example, any>, Required<Example>>>,
Assert<IsExact<MarkRequired<Example, RequiredKeys<Example>>, Example>>,
Assert<IsExact<MarkRequired<Example, OptionalKeys<Example>>, Required<Example>>>,
Assert<
Expand Down
1 change: 1 addition & 0 deletions test/mark-writable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function testMarkWritable() {

type cases = [
Assert<IsExact<MarkWritable<Example, never>, Example>>,
Assert<IsExact<MarkWritable<Example, any>, Writable<Example>>>,
Assert<IsExact<MarkWritable<Example, WritableKeys<Example>>, Example>>,
Assert<IsExact<MarkWritable<Example, ReadonlyKeys<Example>>, Writable<Example>>>,
Assert<IsExact<ReadonlyKeys<ExampleWithWritableReadonly1>, "readonly2">>,
Expand Down

0 comments on commit d02bf22

Please sign in to comment.