Skip to content

Commit 48ddc4b

Browse files
MahinAnowarsom-sm
andauthored
Writable: Fix behavior with index signatures (#1470)
Co-authored-by: Som Shekhar Mukherjee <49264891+som-sm@users.noreply.github.com>
1 parent 5339fb3 commit 48ddc4b

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

source/writable.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ export type Writable<BaseType, Keys extends keyof BaseType = keyof BaseType> =
6161
: Simplify<
6262
// Pick just the keys that are not writable from the base type.
6363
Except<BaseType, Keys>
64-
// Pick the keys that should be writable from the base type and make them writable by removing the `readonly` modifier from the key.
65-
& {-readonly [KeyType in keyof Pick<BaseType, Keys>]: Pick<BaseType, Keys>[KeyType]}
64+
// Make the specified keys writable.
65+
& {-readonly [KeyType in keyof BaseType as KeyType extends Keys ? KeyType : never]: BaseType[KeyType]}
6666
>;
6767

6868
export {};

test-d/writable.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,14 @@ expectType<Set<string>>(variation9);
4848
// Test readonly map
4949
declare const variation10: Writable<ReadonlyMap<string, number>>;
5050
expectType<Map<string, number>>(variation10);
51+
52+
// Only strip `readonly` without otherwise changing the structure, so an index signature is preserved (https://github.com/sindresorhus/type-fest/issues/717).
53+
declare const variation11: Writable<{readonly [key: string]: number}>;
54+
expectType<{[key: string]: number}>(variation11);
55+
56+
// Preserve an index signature alongside named keys while stripping `readonly`.
57+
declare const variation12: Writable<{readonly [key: string]: number; readonly foo: number}>;
58+
expectType<{[key: string]: number; foo: number}>(variation12);
59+
60+
declare const variation13: Writable<{readonly [key: string]: number; readonly foo: number}, 'foo'>;
61+
expectType<{readonly [key: string]: number; foo: number}>(variation13);

0 commit comments

Comments
 (0)