/
ConstructorType.ts
31 lines (23 loc) · 1.45 KB
/
ConstructorType.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Utility Type for JinFrame. ConstructorType help to create constructor parameter.
* This tips from [This article](https://stackoverflow.com/questions/55479658/how-to-create-a-type-excluding-instance-methods-from-a-class-in-typescript) and
* [TypeScript: Create a condition-based subset types - DailyJS](https://medium.com/dailyjs/typescript-create-a-condition-based-subset-types-9d902cea5b8c)
*/
import type { AbstractJinFrame } from '@frames/AbstractJinFrame';
// 1 Transform the type to flag all the undesired keys as 'never'
type FlagExcludedType<Base, Type> = { [Key in keyof Base]: Base[Key] extends Type ? never : Key };
// 2 Get the keys that are not flagged as 'never'
type AllowedNames<Base, Type> = FlagExcludedType<Base, Type>[keyof Base];
// 3 Use this with a simple Pick to get the right interface, excluding the undesired type
type OmitType<Base, Type> = Pick<Base, AllowedNames<Base, Type>>;
// 4 Exclude the Function type to only get properties
export type ConstructorType<T> = OmitType<T, Function>;
export type OmitConstructorType<T, M extends keyof ConstructorType<T>> = Omit<ConstructorType<T>, M>;
export type JinConstructorType<T extends AbstractJinFrame> = Omit<
ConstructorType<T>,
'host' | 'path' | 'method' | 'contentType' | 'customBody'
>;
export type JinOmitConstructorType<T extends AbstractJinFrame, M extends keyof ConstructorType<T>> = Omit<
ConstructorType<T>,
'host' | 'path' | 'method' | 'contentType' | 'customBody' | M
>;