@greenymcgee/utility-types 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +64 -0
- package/index.d.ts +28 -0
- package/package.json +6 -0
- package/tsconfig.json +16 -0
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# Utility Types
|
|
2
|
+
|
|
3
|
+
These are a set of utility types that I use in my projects.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# npm
|
|
9
|
+
npm install -D @greenymcgee/utility-types
|
|
10
|
+
|
|
11
|
+
# pnpm
|
|
12
|
+
pnpm add -D @greenymcgee/utility-types
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
I suggest adding a reference in a top level `types.d.ts` file.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
/// <reference types="@greenymcgee/utility-types" />
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Types
|
|
24
|
+
|
|
25
|
+
### FirstParameterOf
|
|
26
|
+
|
|
27
|
+
Used to get the first parameter of a function.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
type YourType = FirstParameterOf<typeof yourFunction>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### SecondParameterOf
|
|
34
|
+
|
|
35
|
+
Used to get the second parameter of a function.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
type YourType = SecondParameterOf<typeof yourFunction>
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### ThirdParameterOf
|
|
42
|
+
|
|
43
|
+
Used to get the third parameter of a function.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
type YourType = ThirdParameterOf<typeof yourFunction>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### PropsOf
|
|
50
|
+
|
|
51
|
+
Used to get the props of a component.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
type YourProps = PropsOf<typeof YourComponent>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### OneOf
|
|
58
|
+
|
|
59
|
+
Used to get the first element of an array. Very useful if you have
|
|
60
|
+
an array of typed objects, but don't need a type for the objects themselves.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
type YourType = OneOf<typeof yourArray>
|
|
64
|
+
```
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Utility types for grabbing the first three parameters of a method.
|
|
5
|
+
*/
|
|
6
|
+
type FirstParameterOf<Method extends (...args: any) => any> =
|
|
7
|
+
Parameters<Method>[0];
|
|
8
|
+
type SecondParameterOf<Method extends (...args: any) => any> =
|
|
9
|
+
Parameters<Method>[1];
|
|
10
|
+
type ThirdParameterOf<Method extends (...args: any) => any> =
|
|
11
|
+
Parameters<Method>[2];
|
|
12
|
+
|
|
13
|
+
type ConstructorParams<Constructor extends new (...args: any) => any> =
|
|
14
|
+
Constructor extends new (...args: infer Params) => any ? Params : never;
|
|
15
|
+
|
|
16
|
+
type FirstConstructorParameterOf<Klass extends new (...args: any) => any> =
|
|
17
|
+
ConstructorParams<Klass>[0];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* An alias for FirstParameterOf. It's a littles easier to read when working
|
|
21
|
+
* with components.
|
|
22
|
+
*/
|
|
23
|
+
type PropsOf<Component extends (...args: any) => any> =
|
|
24
|
+
FirstParameterOf<Component>;
|
|
25
|
+
|
|
26
|
+
type OneOf<AnyArray extends Array<unknown>> = AnyArray[0];
|
|
27
|
+
|
|
28
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
package/package.json
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
{
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"target": "ES2022",
|
|
5
|
+
"allowJs": false,
|
|
6
|
+
"strict": true,
|
|
7
|
+
"noEmit": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"moduleResolution": "bundler",
|
|
10
|
+
"resolveJsonModule": true,
|
|
11
|
+
"isolatedModules": true,
|
|
12
|
+
"incremental": true,
|
|
13
|
+
},
|
|
14
|
+
"include": ["**/*.ts"],
|
|
15
|
+
"exclude": ["node_modules"]
|
|
16
|
+
}
|