mirror of
https://github.com/sindresorhus/type-fest.git
synced 2025-12-08 19:25:05 +00:00
PartialDeep: Skip constructor (#730)
This commit is contained in:
parent
3ee234ad18
commit
75edeefd4a
26
source/partial-deep.d.ts
vendored
26
source/partial-deep.d.ts
vendored
@ -59,7 +59,7 @@ const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true}> = {
|
||||
@category Set
|
||||
@category Map
|
||||
*/
|
||||
export type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIns
|
||||
export type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIns | (((...arguments_: any[]) => unknown)) | (new (...arguments_: any[]) => unknown)
|
||||
? T
|
||||
: T extends Map<infer KeyType, infer ValueType>
|
||||
? PartialMapDeep<KeyType, ValueType, Options>
|
||||
@ -69,19 +69,17 @@ export type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends
|
||||
? PartialReadonlyMapDeep<KeyType, ValueType, Options>
|
||||
: T extends ReadonlySet<infer ItemType>
|
||||
? PartialReadonlySetDeep<ItemType, Options>
|
||||
: T extends ((...arguments_: any[]) => unknown)
|
||||
? T | undefined
|
||||
: T extends object
|
||||
? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
|
||||
? Options['recurseIntoArrays'] extends true
|
||||
? ItemType[] extends T // Test for arrays (non-tuples) specifically
|
||||
? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
|
||||
? ReadonlyArray<PartialDeep<ItemType | undefined, Options>>
|
||||
: Array<PartialDeep<ItemType | undefined, Options>>
|
||||
: PartialObjectDeep<T, Options> // Tuples behave properly
|
||||
: T // If they don't opt into array testing, just use the original type
|
||||
: PartialObjectDeep<T, Options>
|
||||
: unknown;
|
||||
: T extends object
|
||||
? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
|
||||
? Options['recurseIntoArrays'] extends true
|
||||
? ItemType[] extends T // Test for arrays (non-tuples) specifically
|
||||
? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
|
||||
? ReadonlyArray<PartialDeep<ItemType | undefined, Options>>
|
||||
: Array<PartialDeep<ItemType | undefined, Options>>
|
||||
: PartialObjectDeep<T, Options> // Tuples behave properly
|
||||
: T // If they don't opt into array testing, just use the original type
|
||||
: PartialObjectDeep<T, Options>
|
||||
: unknown;
|
||||
|
||||
/**
|
||||
Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
|
||||
|
||||
@ -1,10 +1,15 @@
|
||||
import {expectType, expectError, expectAssignable} from 'tsd';
|
||||
import type {PartialDeep} from '../index';
|
||||
|
||||
class ClassA {
|
||||
foo = 1;
|
||||
}
|
||||
|
||||
const foo = {
|
||||
baz: 'fred',
|
||||
bar: {
|
||||
function: (_: string): void => undefined,
|
||||
classConstructor: ClassA,
|
||||
object: {key: 'value'},
|
||||
string: 'waldo',
|
||||
number: 1,
|
||||
@ -30,6 +35,11 @@ let partialDeepFoo: PartialDeep<typeof foo, {recurseIntoArrays: true}> = foo;
|
||||
expectError(expectType<Partial<typeof foo>>(partialDeepFoo));
|
||||
const partialDeepBar: PartialDeep<typeof foo.bar, {recurseIntoArrays: true}> = foo.bar;
|
||||
expectType<typeof partialDeepBar | undefined>(partialDeepFoo.bar);
|
||||
// Check for constructor
|
||||
expectType<typeof ClassA | undefined>(partialDeepFoo.bar!.classConstructor);
|
||||
const instance = new partialDeepFoo.bar!.classConstructor!();
|
||||
instance.foo = 2;
|
||||
const b = partialDeepFoo.bar!.constructor;
|
||||
expectType<((_: string) => void) | undefined>(partialDeepFoo.bar!.function);
|
||||
expectAssignable<object | undefined>(partialDeepFoo.bar!.object);
|
||||
expectType<string | undefined>(partialDeepFoo.bar!.string);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user