Skip to content

Commit 200356c

Browse files
committed
Add common types.
1 parent b91025a commit 200356c

10 files changed

Lines changed: 87 additions & 2 deletions

File tree

ROADMAP.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# typed
2+
3+
[Better typings for strings and numbers in TypeString using opaque types](https://medium.com/hypersphere-codes/better-types-for-strings-and-numbers-in-typestring-using-opaque-types-c9153eb8009c)
4+
5+
- `utility-types` provides Brand<T, U>
6+
7+
- `ts-toolbelt` provides Type<A, Id>
8+
9+
- `ts-essentials` provides Opaque<T, Key>
10+
11+
- `ts-brand` which provides more advanced functionality for branding
12+
13+
- `type-fest` remove or rename duplicates from this one

common/index.d.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
export type PrimitiveType = bigint | boolean | null | number | string | symbol | undefined;
2+
3+
export type NonPrimitiveType = object;
4+
5+
export type MergeType<T> = {
6+
[P in keyof T]: T[P];
7+
};
8+
9+
/**
10+
* @example
11+
* interface User {
12+
* name: string;
13+
* age: number;
14+
* address: string;
15+
* }
16+
* type UserPartialName = PartialByKeysType<User, "name">;
17+
* { name?:string; age:number; address:string }
18+
*/
19+
export type PartialByKeysType<T, K = keyof T> = MergeType<
20+
{
21+
[P in keyof T as P extends K ? P : never]?: T[P];
22+
} & {
23+
[P in keyof T as P extends K ? never : P]: T[P];
24+
}
25+
>;
26+
27+
export type PartialDeepObjectType<T> = T extends object
28+
? {
29+
[P in keyof T]?: PartialDeepObjectType<T[P]>;
30+
}
31+
: T;
32+
33+
export type PartialDeepType<T> = T extends Date | bigint | boolean | null | number | string | symbol | undefined
34+
? T | undefined
35+
: T extends Array<infer ArrayType>
36+
? Array<PartialDeepType<ArrayType>>
37+
: T extends ReadonlyArray<infer ArrayType>
38+
? ReadonlyArray<ArrayType>
39+
: T extends Set<infer SetType>
40+
? Set<PartialDeepType<SetType>>
41+
: T extends ReadonlySet<infer SetType>
42+
? ReadonlySet<SetType>
43+
: T extends Map<infer KeyType, infer ValueType>
44+
? Map<PartialDeepType<KeyType>, PartialDeepType<ValueType>>
45+
: T extends ReadonlyMap<infer KeyType, infer ValueType>
46+
? ReadonlyMap<PartialDeepType<KeyType>, PartialDeepType<ValueType>>
47+
: {
48+
[K in keyof T]?: PartialDeepType<T[K]>;
49+
};

docs/common/common.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Common
2+
3+
## Coomon types
4+
5+
_Since `1.4.0`_
6+
7+
```typescript
8+
import type {
9+
MergeType,
10+
NonPrimitiveType,
11+
PartialByKeysType,
12+
PartialDeepObjectType,
13+
PartialDeepType,
14+
PrimitiveType,
15+
} from "@corefunc/type/common";
16+
```

index.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
1515
};
1616
exports.__esModule = true;
1717
__exportStar(require("./class"), exports);
18+
__exportStar(require("./common"), exports);
1819
__exportStar(require("./json"), exports);
1920
__exportStar(require("./number"), exports);
2021
__exportStar(require("./object"), exports);

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./class";
2+
export * from "./common";
23
export * from "./json";
34
export * from "./number";
45
export * from "./object";

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
1515
};
1616
exports.__esModule = true;
1717
__exportStar(require("./class"), exports);
18+
__exportStar(require("./common"), exports);
1819
__exportStar(require("./json"), exports);
1920
__exportStar(require("./number"), exports);
2021
__exportStar(require("./object"), exports);

index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./class";
2+
export * from "./common";
23
export * from "./json";
34
export * from "./number";
45
export * from "./object";

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./class";
2+
export * from "./common";
23
export * from "./json";
34
export * from "./number";
45
export * from "./object";

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"class/key-of-class.d.ts",
3333
"class/key-of-class.js",
3434
"class/key-of-class.mjs",
35+
"common/index.d.ts",
3536
"json/index.d.ts",
3637
"number/float-range.type.js",
3738
"number/float-range.type.d.ts",
@@ -89,5 +90,5 @@
8990
"test": "echo \"Error: no test specified\" && exit 1"
9091
},
9192
"types": "./index.d.ts",
92-
"version": "1.3.7"
93+
"version": "1.4.0"
9394
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3-
"declaration": true
3+
"declaration": true,
4+
"lib": ["ES2021"],
45
}
56
}

0 commit comments

Comments
 (0)