Skip to content

AtLeastOneExtend #1379

@salisbury-espinosa

Description

@salisbury-espinosa

related PR : #1378

we can just modify
https://github.com/sindresorhus/type-fest/blob/main/source/all-extend.d.ts#L114

First extends Type
				? _AllExtend<Rest, Type, Options>
				: false

=>

First extends Type
          ? true
          : _AllExtend<Rest, Type, Options>

but most likely a base type is needed, where will the option be mode = 'all' | 'atLeaseOne' which will already be used by these types.

unit tests example:

// Basic matches
expectType<AtLeastOneExtend<[1, 2, 3], number>>(true);
expectType<AtLeastOneExtend<[1, '2', 3], string>>(true);
expectType<AtLeastOneExtend<[1, 2, 3], string>>(false);
expectType<AtLeastOneExtend<[1, 2, 3], number>>(true);
expectType<AtLeastOneExtend<[1, 2, '3'], number>>(true);
expectType<AtLeastOneExtend<[1, 2, '3'], string>>(true);
expectType<AtLeastOneExtend<['1', '2', '3'], number>>(false);
expectType<AtLeastOneExtend<['a', 'b', 'c'], string>>(true);

// Optional Elements
expectType<AtLeastOneExtend<[1?, 2?], number>>(true);
expectType<AtLeastOneExtend<[number?, string?], string>>(true);
expectType<AtLeastOneExtend<[number?], string>>(false);

/ Edge case: Checking against any[] specifically
expectType<AtLeastOneExtend<[Array<number>, string], Array<any>>>(true);
expectType<AtLeastOneExtend<[number, string], Array<any>>>(false);

/* Edge case: Checking against never[] specifically
   Only never[] (or any) will extend never[] */
expectType<AtLeastOneExtend<[Array<never>, number], Array<never>>>(true);
expectType<AtLeastOneExtend<[Array<number>, string], Array<never>>>(false);

// Basic string array check
expectType<AtLeastOneExtend<[Array<string>, number], Array<string>>>(true);

// Multiple matches
expectType<
  AtLeastOneExtend<[Array<string>, Array<string>, boolean], Array<string>>
>(true);

// Partial union match (Union containing string[])
expectType<AtLeastOneExtend<[number, Array<string> | number], Array<string>>>(
  true,
);

// Negative case (Array of different type)
expectType<AtLeastOneExtend<[Array<number>, boolean], Array<string>>>(false);

// Negative case (Individual string does not extend string[])
expectType<AtLeastOneExtend<['hello', number], Array<string>>>(false);

// Readonly string array check
expectType<AtLeastOneExtend<[ReadonlyArray<string>, number], Array<string>>>(
  true,
);

// Tuple that extends string[]
expectType<AtLeastOneExtend<[['a', 'b'], number], Array<string>>>(true);

// Intersections
expectType<AtLeastOneExtend<[{ a: 1 } & { b: 2 }, { c: 3 }], { a: 1 }>>(true);
expectType<AtLeastOneExtend<[{ a: 1 }, { b: 2 }], { a: 1 } & { b: 2 }>>(false);

Type source

No response

Search existing types and issues first

  • I tried my best to look for it

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions