Skip to content

Instantly share code, notes, and snippets.

@LayZeeDK
Last active August 26, 2024 20:28
Show Gist options
  • Save LayZeeDK/ee40eb0efa57d1ac5b9a476700e4adbf to your computer and use it in GitHub Desktop.
Save LayZeeDK/ee40eb0efa57d1ac5b9a476700e4adbf to your computer and use it in GitHub Desktop.
TypeScript utility types

TypeScript utility types

License (MIT)

Copyright 2024 Lars Gyrup Brink Nielsen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import type { Type } from '@angular/core';
/**
* Strongly typed input properties for `NgComponentOutlet`, including
* `component` and `inputs`.
*
* @example
* type AppComponentOutlet =
* ComponentOutletWithInputs<HeroComponent | VillainComponent>;
* // -> {
* readonly component: Type<HeroComponent>;
* readonly inputs: Partial<HeroComponent>;
* } | {
* readonly component: Type<VillainComponent>;
* readonly inputs: Partial<VillainComponent>;
* }
*/
export type ComponentOutletWithInputs<TComponent> =
TComponent extends infer TInferredComponent
? {
readonly component: Type<TInferredComponent>;
readonly inputs: Partial<TInferredComponent>;
}
: never;
/**
* A record of member names.
*
* @example
* interface Person {
* readonly address: Address;
* readonly age: number;
* readonly name: string;
* }
*
* const personPropertyNames: PropertyNames<Person> = {
* address: 'address',
* age: 'age',
* name: 'name',
* };
* const commaSeparatedlistOfPropertyNames = [
* personPropertyNames.address,
* personPropertyNames.age,
* personPropertyNames.name,
* ].join(',');
*
* @license
* Copyright 2023 Lars Gyrup Brink Nielsen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export type MemberRecord<TDataStructure> = Record<
keyof TDataStructure,
keyof TDataStructure
>;
/**
* Extract the type of a single public member.
*
* @example
* interface PersonFromSomeLibrary {
* readonly name: string;
* readonly address: AddressFromSomeLibrary;
* }
* type Address = MemberType<PersonFromSomeLibrary, 'address'>;
*
* function formatAddress(address: Address) {
* // (...)
* }
*
* @license
* Copyright 2023 Lars Gyrup Brink Nielsen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export type MemberType<
TInterface,
TMemberKey extends keyof TInterface
> = TInterface[TMemberKey];
import type { ComponentStore } from '@ngrx/component-store';
import type { PublicMembers } from './public-members';
/**
* Extract public members that are not inherited from `ComponentStore`, that is the facade.
*
* @license
* Copyright 2023 Lars Gyrup Brink Nielsen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export type NonComponentStoreMembers<
TComponentStore extends ComponentStore<TState>,
TState extends object = object
> = Omit<
PublicMembers<TComponentStore>,
| 'destroy$'
| 'effect'
| 'ngOnDestroy'
| 'ngrxOnStateInit'
| 'ngrxOnStoreInit'
| 'patchState'
| 'select'
| 'selectSignal'
| 'setState'
| 'state'
| 'state$'
| 'updater'
>;
import type {
EntityComponentStore,
EntityState,
ExtractEntity,
ExtractId,
} from '@rx-mind/entity-component-store';
import type { PublicMembers } from './public-members';
/**
* Extract public members that are not inherited from `EntityComponentStore`,
* that is the facade.
*
* @license
* Copyright 2023 Lars Gyrup Brink Nielsen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export type NonEntityComponentStoreMembers<
TEntityComponentStore extends EntityComponentStore<TState, TEntity, TId>,
TState extends EntityState<TEntity, TId>,
TEntity extends Record<string, unknown> = ExtractEntity<TState>,
TId extends string | number = ExtractId<TState>
> = Omit<
PublicMembers<TEntityComponentStore>,
//
// EntityComponentStore
//
| 'addOne'
| 'addMany'
| 'all$'
| 'entities$'
| 'ids$'
| 'setAll'
| 'setMany'
| 'setOne'
| 'removeAll'
| 'removeMany'
| 'removeOne'
| 'updateMany'
| 'updateOne'
| 'upsertMany'
| 'upsertOne'
| 'map'
| 'mapOne'
| 'total$'
//
// ComponentStore
//
| 'destroy$'
| 'effect'
| 'ngOnDestroy'
| 'ngrxOnStateInit'
| 'ngrxOnStoreInit'
| 'patchState'
| 'select'
| 'selectSignal'
| 'setState'
| 'state'
| 'state$'
| 'updater'
>;
/**
* Shallow removal of `null` and `undefined` from the types of all members of a shape.
*
* @example
* interface LaxPerson {
* name?: string;
* address?: Address | null;
* age: number | null;
* }
* type StrictPerson = NonNullish<LaxPerson>;
* // -> interface {
* // name: string;
* // address: Address;
* // age: number;
* // }
*
* @license
* Copyright 2023 Lars Gyrup Brink Nielsen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export type NonNullish<TShape> = {
[TMember in keyof Required<TShape>]: NonNullable<TShape[TMember]>;
};
/**
* * Add `null` to the types of all members of a shape.
*
* @example
* interface StrictPerson {
* name: string;
* address: Address;
* age: number;
* }
* type LaxPerson = Nullable<StrictPerson>;
* // -> interface {
* // name: string | null;
* // address: Address | null;
* // age: number | null;
* // }
*
* @license
* Copyright 2023 Lars Gyrup Brink Nielsen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export type Nullable<TShape> = { [TMemberKey in keyof TShape]: TShape[TMemberKey] | null };
/**
* Convert the specified member(s) to optional (specified type or `undefined`).
*
* @example <caption>Person interface.</caption>
* interface Person {
* firstName: string;
* middleName: string;
* lastName: string;
* }
* @example <caption>Convert single member to optional.</caption>
* type PersonWithOptionalMiddlename = Optional<Person, 'middleName'>;
* // -> interface {
* // firstName: string;
* // middleName?: string;
* // lastName: string;
* // }
* @example <caption>Convert multiple members to optional.</caption>
* type PersonWithOptionalMiddlenameAndLastName = Optional<Person, 'middleName' | 'lastName'>;
* // -> interface {
* // firstName: string;
* // middleName?: string;
* // lastName?: string;
* // }
*
* @license
* Copyright 2023 Lars Gyrup Brink Nielsen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export type Optional<TInterface, TMemberKey extends keyof TInterface> = Omit<
TInterface,
TMemberKey
> &
Pick<Partial<TInterface>, TMemberKey>;
/**
* Shallow override of one or more members of a shape.
*
* @remarks Use the `OverrideProperties` type from the `type-fest` package for better typing.
*
* @example
* interface LaxPerson {
* name?: string;
* address?: Address | null;
* age: number | null;
* }
* type NamedPerson = Override<
* LaxPerson,
* {
* name: string;
* }
* >;
* // -> interface {
* // name: string;
* // address?: Address | null;
* // age: number | null;
* // }
*
* @license
* Copyright 2023 Lars Gyrup Brink Nielsen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export type Override<TOriginal, TOverrides> = Omit<
TOriginal,
keyof TOverrides
> &
TOverrides;
/**
* Extract public members of a class by excluding private and protected members.
*
* @example
* class TimeTravelVehicle {
* destination: Date;
* origin: Date;
* power: number;
* speed: number;
*
* constructor(private driver: Driver, private flux: FluxCapacitor) {}
*
* greatScott(): void {
* // (...)
* }
* }
*
* class TimeTravelStub implements PublicMembers<TimeTravelVehicle> {
* destination: new Date(Date().getTime() - 24 * 60 * 60 * 1000);
* origin: Date = new Date();
* power = 1.21;
* speed = 88;
*
* greatScott(): void {
* // No-op
* }
* }
*
* @license
* Copyright 2023 Lars Gyrup Brink Nielsen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export type PublicMembers<TClass> = {
[TPublicMemberKey in keyof TClass]: TClass[TPublicMemberKey];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment