Skip to content

Commit

Permalink
ReadonlyDeep: Skip constructor (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiyaaaaa authored Oct 24, 2023
1 parent 9baccc3 commit 3ee234a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
44 changes: 23 additions & 21 deletions source/readonly-deep.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,30 @@ Note that types containing overloaded functions are not made deeply readonly due
*/
export type ReadonlyDeep<T> = T extends BuiltIns
? T
: T extends (...arguments_: any[]) => unknown
? {} extends ReadonlyObjectDeep<T>
? T
: HasMultipleCallSignatures<T> extends true
: T extends new (...args: any[]) => unknown
? T // Skip class constructors
: T extends (...arguments_: any[]) => unknown
? {} extends ReadonlyObjectDeep<T>
? T
: ((...arguments_: Parameters<T>) => ReturnType<T>) & ReadonlyObjectDeep<T>
: T extends Readonly<ReadonlyMap<infer KeyType, infer ValueType>>
? ReadonlyMapDeep<KeyType, ValueType>
: T extends Readonly<ReadonlySet<infer ItemType>>
? ReadonlySetDeep<ItemType>
: // Identify tuples to avoid converting them to arrays inadvertently; special case `readonly [...never[]]`, as it emerges undesirably from recursive invocations of ReadonlyDeep below.
T extends readonly [] | readonly [...never[]]
? readonly []
: T extends readonly [infer U, ...infer V]
? readonly [ReadonlyDeep<U>, ...ReadonlyDeep<V>]
: T extends readonly [...infer U, infer V]
? readonly [...ReadonlyDeep<U>, ReadonlyDeep<V>]
: T extends ReadonlyArray<infer ItemType>
? ReadonlyArray<ReadonlyDeep<ItemType>>
: T extends object
? ReadonlyObjectDeep<T>
: unknown;
: HasMultipleCallSignatures<T> extends true
? T
: ((...arguments_: Parameters<T>) => ReturnType<T>) & ReadonlyObjectDeep<T>
: T extends Readonly<ReadonlyMap<infer KeyType, infer ValueType>>
? ReadonlyMapDeep<KeyType, ValueType>
: T extends Readonly<ReadonlySet<infer ItemType>>
? ReadonlySetDeep<ItemType>
: // Identify tuples to avoid converting them to arrays inadvertently; special case `readonly [...never[]]`, as it emerges undesirably from recursive invocations of ReadonlyDeep below.
T extends readonly [] | readonly [...never[]]
? readonly []
: T extends readonly [infer U, ...infer V]
? readonly [ReadonlyDeep<U>, ...ReadonlyDeep<V>]
: T extends readonly [...infer U, infer V]
? readonly [...ReadonlyDeep<U>, ReadonlyDeep<V>]
: T extends ReadonlyArray<infer ItemType>
? ReadonlyArray<ReadonlyDeep<ItemType>>
: T extends object
? ReadonlyObjectDeep<T>
: unknown;

/**
Same as `ReadonlyDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `ReadonlyDeep`.
Expand Down
9 changes: 9 additions & 0 deletions test-d/readonly-deep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ type ReadonlyJsonValue =
| boolean
| null;

class ClassA {
foo = 1;
}

const data = {
object: {
foo: 'bar',
},
constructor: ClassA,
fn: (_: string) => true,
fnWithOverload: ((_: number) => 'foo') as Overloaded,
namespace: {} as unknown as Namespace,
Expand Down Expand Up @@ -66,6 +71,10 @@ readonlyData.fn('foo');
readonlyData.fnWithOverload(1);
readonlyData.fnWithOverload('', 1);

expectType<typeof ClassA>(readonlyData.constructor);
const instance = new readonlyData.constructor();
instance.foo = 2; // Constructor is not made readonly

expectError(readonlyData.string = 'bar');
expectType<{readonly foo: string}>(readonlyData.object);
expectType<string>(readonlyData.string);
Expand Down

0 comments on commit 3ee234a

Please sign in to comment.