@layerzerolabs/typescript-utils 0.0.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/.turbo/turbo-build.log +159 -0
- package/.turbo/turbo-lint.log +4 -0
- package/dist/3OBHE7MG.js +6 -0
- package/dist/3OBHE7MG.js.map +1 -0
- package/dist/44IBECAR.cjs +8 -0
- package/dist/44IBECAR.cjs.map +1 -0
- package/dist/4RNWLMHM.js +45 -0
- package/dist/4RNWLMHM.js.map +1 -0
- package/dist/62A6WP5R.js +6 -0
- package/dist/62A6WP5R.js.map +1 -0
- package/dist/7MFOZW3L.js +6 -0
- package/dist/7MFOZW3L.js.map +1 -0
- package/dist/AMF2EES3.cjs +1332 -0
- package/dist/AMF2EES3.cjs.map +1 -0
- package/dist/ATSF4XM5.cjs +8 -0
- package/dist/ATSF4XM5.cjs.map +1 -0
- package/dist/AYCOZKHR.js +8435 -0
- package/dist/AYCOZKHR.js.map +1 -0
- package/dist/B77ZZJ5J.cjs +8 -0
- package/dist/B77ZZJ5J.cjs.map +1 -0
- package/dist/BLALMGVL.js +6 -0
- package/dist/BLALMGVL.js.map +1 -0
- package/dist/BPWNPHCG.cjs +8 -0
- package/dist/BPWNPHCG.cjs.map +1 -0
- package/dist/C2AO2CD6.js +6 -0
- package/dist/C2AO2CD6.js.map +1 -0
- package/dist/DXU6T5SS.js +6 -0
- package/dist/DXU6T5SS.js.map +1 -0
- package/dist/FPWOFXCL.js +6 -0
- package/dist/FPWOFXCL.js.map +1 -0
- package/dist/FQTDMGIM.cjs +8 -0
- package/dist/FQTDMGIM.cjs.map +1 -0
- package/dist/GHCFU27L.js +6 -0
- package/dist/GHCFU27L.js.map +1 -0
- package/dist/GKC44C4P.cjs +8 -0
- package/dist/GKC44C4P.cjs.map +1 -0
- package/dist/HKAX3PXE.cjs +8 -0
- package/dist/HKAX3PXE.cjs.map +1 -0
- package/dist/HVKBLL5V.js +6 -0
- package/dist/HVKBLL5V.js.map +1 -0
- package/dist/JRGCWPQC.cjs +8 -0
- package/dist/JRGCWPQC.cjs.map +1 -0
- package/dist/KGYIWVDZ.cjs +8 -0
- package/dist/KGYIWVDZ.cjs.map +1 -0
- package/dist/KLHW5WN3.cjs +8 -0
- package/dist/KLHW5WN3.cjs.map +1 -0
- package/dist/M2FMGCRF.cjs +8 -0
- package/dist/M2FMGCRF.cjs.map +1 -0
- package/dist/MNZ3MKHF.js +6 -0
- package/dist/MNZ3MKHF.js.map +1 -0
- package/dist/N3MJTAM7.js +6 -0
- package/dist/N3MJTAM7.js.map +1 -0
- package/dist/Q3ERQ4ME.cjs +8438 -0
- package/dist/Q3ERQ4ME.cjs.map +1 -0
- package/dist/Q3ZAC463.js +6 -0
- package/dist/Q3ZAC463.js.map +1 -0
- package/dist/Q74ZLWCL.cjs +8 -0
- package/dist/Q74ZLWCL.cjs.map +1 -0
- package/dist/TDMZOP4J.js +6 -0
- package/dist/TDMZOP4J.js.map +1 -0
- package/dist/TML6PHUW.js +6 -0
- package/dist/TML6PHUW.js.map +1 -0
- package/dist/U7VZULNU.cjs +51 -0
- package/dist/U7VZULNU.cjs.map +1 -0
- package/dist/VLOINXK2.cjs +8 -0
- package/dist/VLOINXK2.cjs.map +1 -0
- package/dist/XM743A75.js +1326 -0
- package/dist/XM743A75.js.map +1 -0
- package/dist/Y53SFKBY.cjs +8 -0
- package/dist/Y53SFKBY.cjs.map +1 -0
- package/dist/ZLF3Y44J.cjs +8 -0
- package/dist/ZLF3Y44J.cjs.map +1 -0
- package/dist/ZPWIEOJR.js +6 -0
- package/dist/ZPWIEOJR.js.map +1 -0
- package/dist/advancedRecord.cjs +7 -0
- package/dist/advancedRecord.cjs.map +1 -0
- package/dist/advancedRecord.d.ts +10 -0
- package/dist/advancedRecord.d.ts.map +1 -0
- package/dist/advancedRecord.js +4 -0
- package/dist/advancedRecord.js.map +1 -0
- package/dist/branded.cjs +7 -0
- package/dist/branded.cjs.map +1 -0
- package/dist/branded.d.ts +22 -0
- package/dist/branded.d.ts.map +1 -0
- package/dist/branded.js +4 -0
- package/dist/branded.js.map +1 -0
- package/dist/constructor.cjs +7 -0
- package/dist/constructor.cjs.map +1 -0
- package/dist/constructor.d.ts +4 -0
- package/dist/constructor.d.ts.map +1 -0
- package/dist/constructor.js +4 -0
- package/dist/constructor.js.map +1 -0
- package/dist/deep.cjs +7 -0
- package/dist/deep.cjs.map +1 -0
- package/dist/deep.d.ts +22 -0
- package/dist/deep.d.ts.map +1 -0
- package/dist/deep.js +4 -0
- package/dist/deep.js.map +1 -0
- package/dist/deep.test-d.cjs +15 -0
- package/dist/deep.test-d.cjs.map +1 -0
- package/dist/deep.test-d.d.ts +2 -0
- package/dist/deep.test-d.d.ts.map +1 -0
- package/dist/deep.test-d.js +13 -0
- package/dist/deep.test-d.js.map +1 -0
- package/dist/disallowedAny.cjs +7 -0
- package/dist/disallowedAny.cjs.map +1 -0
- package/dist/disallowedAny.d.ts +5 -0
- package/dist/disallowedAny.d.ts.map +1 -0
- package/dist/disallowedAny.js +4 -0
- package/dist/disallowedAny.js.map +1 -0
- package/dist/identity.cjs +7 -0
- package/dist/identity.cjs.map +1 -0
- package/dist/identity.d.ts +2 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +4 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.cjs +23 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/lastOf.cjs +7 -0
- package/dist/lastOf.cjs.map +1 -0
- package/dist/lastOf.d.ts +7 -0
- package/dist/lastOf.d.ts.map +1 -0
- package/dist/lastOf.js +4 -0
- package/dist/lastOf.js.map +1 -0
- package/dist/merge.cjs +7 -0
- package/dist/merge.cjs.map +1 -0
- package/dist/merge.d.ts +7 -0
- package/dist/merge.d.ts.map +1 -0
- package/dist/merge.js +4 -0
- package/dist/merge.js.map +1 -0
- package/dist/methodOf.cjs +7 -0
- package/dist/methodOf.cjs.map +1 -0
- package/dist/methodOf.d.ts +7 -0
- package/dist/methodOf.d.ts.map +1 -0
- package/dist/methodOf.js +4 -0
- package/dist/methodOf.js.map +1 -0
- package/dist/removeNever.cjs +7 -0
- package/dist/removeNever.cjs.map +1 -0
- package/dist/removeNever.d.ts +4 -0
- package/dist/removeNever.d.ts.map +1 -0
- package/dist/removeNever.js +4 -0
- package/dist/removeNever.js.map +1 -0
- package/dist/strings.cjs +7 -0
- package/dist/strings.cjs.map +1 -0
- package/dist/strings.d.ts +27 -0
- package/dist/strings.d.ts.map +1 -0
- package/dist/strings.js +4 -0
- package/dist/strings.js.map +1 -0
- package/dist/strings.test-d.cjs +21 -0
- package/dist/strings.test-d.cjs.map +1 -0
- package/dist/strings.test-d.d.ts +2 -0
- package/dist/strings.test-d.d.ts.map +1 -0
- package/dist/strings.test-d.js +19 -0
- package/dist/strings.test-d.js.map +1 -0
- package/dist/tuples.cjs +7 -0
- package/dist/tuples.cjs.map +1 -0
- package/dist/tuples.d.ts +11 -0
- package/dist/tuples.d.ts.map +1 -0
- package/dist/tuples.js +4 -0
- package/dist/tuples.js.map +1 -0
- package/dist/valueOf.cjs +7 -0
- package/dist/valueOf.cjs.map +1 -0
- package/dist/valueOf.d.ts +12 -0
- package/dist/valueOf.d.ts.map +1 -0
- package/dist/valueOf.js +4 -0
- package/dist/valueOf.js.map +1 -0
- package/dist/viem.cjs +7 -0
- package/dist/viem.cjs.map +1 -0
- package/dist/viem.d.ts +186 -0
- package/dist/viem.d.ts.map +1 -0
- package/dist/viem.js +4 -0
- package/dist/viem.js.map +1 -0
- package/dist/withRequired.cjs +7 -0
- package/dist/withRequired.cjs.map +1 -0
- package/dist/withRequired.d.ts +7 -0
- package/dist/withRequired.d.ts.map +1 -0
- package/dist/withRequired.js +4 -0
- package/dist/withRequired.js.map +1 -0
- package/package.json +32 -0
- package/src/advancedRecord.ts +20 -0
- package/src/branded.ts +19 -0
- package/src/constructor.ts +1 -0
- package/src/deep.test-d.ts +33 -0
- package/src/deep.ts +26 -0
- package/src/disallowedAny.ts +5 -0
- package/src/identity.ts +2 -0
- package/src/index.ts +15 -0
- package/src/lastOf.ts +15 -0
- package/src/merge.ts +13 -0
- package/src/methodOf.ts +7 -0
- package/src/removeNever.ts +3 -0
- package/src/strings.test-d.ts +32 -0
- package/src/strings.ts +42 -0
- package/src/tuples.ts +49 -0
- package/src/valueOf.ts +16 -0
- package/src/viem.ts +275 -0
- package/src/withRequired.ts +5 -0
- package/test/disallowedAny.test.ts +54 -0
- package/test/merge.test.ts +27 -0
- package/tsconfig.json +20 -0
- package/tsup.config.ts +8 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withRequired.d.ts","sourceRoot":"","sources":["../src/withRequired.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,CAAC,GAAG;KAAG,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,CAAC;AAE1E,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI;KACxB,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACzB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"withRequired.js"}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@layerzerolabs/typescript-utils",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"require": "./dist/index.cjs",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"default": "./dist/index.cjs"
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/index.cjs",
|
|
14
|
+
"module": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"tsup": "^8.4.0",
|
|
18
|
+
"vitest": "^3.2.3",
|
|
19
|
+
"@layerzerolabs/tsup-configuration": "0.0.1",
|
|
20
|
+
"@layerzerolabs/typescript-configuration": "0.0.0"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "restricted",
|
|
24
|
+
"registry": "https://registry.npmjs.org/"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup",
|
|
28
|
+
"checkdeps": "pnpm --filter @layerzerolabs/depcheck run depcheck validate --catalog --missing-dependencies --only $npm_package_name",
|
|
29
|
+
"clean": "rm -rf ./node_modules .turbo ./dist",
|
|
30
|
+
"lint": "eslint . --max-warnings 0 || (eslint . --fix --max-warnings 0 && false)"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type AdvancedRecord<T = any, U = any> = {
|
|
2
|
+
readonly [key: string]: readonly [T, U];
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
export type DeepWritable<T> = { -readonly [P in keyof T]: DeepWritable<T[P]> };
|
|
6
|
+
|
|
7
|
+
export type AdvancedRecordLookup<
|
|
8
|
+
AR extends AdvancedRecord | undefined,
|
|
9
|
+
KeyToFind,
|
|
10
|
+
> = AR extends AdvancedRecord
|
|
11
|
+
? {
|
|
12
|
+
[P in keyof AR]: AR[P] extends readonly [infer K, infer V]
|
|
13
|
+
? KeyToFind extends DeepWritable<K>
|
|
14
|
+
? V
|
|
15
|
+
: DeepWritable<K> extends KeyToFind
|
|
16
|
+
? V
|
|
17
|
+
: never
|
|
18
|
+
: never;
|
|
19
|
+
}[keyof AR]
|
|
20
|
+
: never;
|
package/src/branded.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const tag = '___tag___';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a branded type of {@link T} with the brand {@link U}.
|
|
4
|
+
*
|
|
5
|
+
* @param T - Type to brand
|
|
6
|
+
* @param U - Label
|
|
7
|
+
* @returns Branded type
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* type Result = Branded<string, 'foo'>
|
|
11
|
+
* // ^? type Result = string & { [symbol]: 'foo' }
|
|
12
|
+
*/
|
|
13
|
+
export type Branded<T, U> = T & { [tag]: U };
|
|
14
|
+
/**
|
|
15
|
+
* Represents a percentage as a decimal value. For example, 25% is represented as 0.25
|
|
16
|
+
*/
|
|
17
|
+
export type Percent = number; // Branded<number, 'percent'>
|
|
18
|
+
|
|
19
|
+
export type BrandedError<T extends string> = Branded<Error, T>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Constructor<T> = { new (...args: any[]): T };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { expectTypeOf, test } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import type { Branded } from './branded';
|
|
4
|
+
import type { DeepOptional } from './deep';
|
|
5
|
+
|
|
6
|
+
test('DeepOptional', () => {
|
|
7
|
+
// 1 level
|
|
8
|
+
type A = DeepOptional<{ a: string; b: number }>;
|
|
9
|
+
expectTypeOf<A>().toEqualTypeOf<{ a?: string; b?: number }>();
|
|
10
|
+
|
|
11
|
+
// 2 levels
|
|
12
|
+
type B = DeepOptional<{ a: { c: string }; b: number }>;
|
|
13
|
+
expectTypeOf<B>().toEqualTypeOf<{ a?: { c?: string }; b?: number }>();
|
|
14
|
+
|
|
15
|
+
// 1 level with brand tag
|
|
16
|
+
type obj = { a: string; b: number };
|
|
17
|
+
type branded = Branded<obj, 'foo'>;
|
|
18
|
+
type C = DeepOptional<branded>;
|
|
19
|
+
expectTypeOf<C>().toEqualTypeOf<{ a?: string; b?: number; ___tag___: 'foo' }>();
|
|
20
|
+
|
|
21
|
+
// 2 levels with brand tag
|
|
22
|
+
type obj2 = { a: branded; b: number };
|
|
23
|
+
type D = DeepOptional<Branded<obj2, 'foo2'>>;
|
|
24
|
+
expectTypeOf<D>().toEqualTypeOf<{
|
|
25
|
+
a?: {
|
|
26
|
+
a?: string;
|
|
27
|
+
b?: number;
|
|
28
|
+
___tag___: 'foo';
|
|
29
|
+
};
|
|
30
|
+
b?: number;
|
|
31
|
+
___tag___: 'foo2';
|
|
32
|
+
}>();
|
|
33
|
+
});
|
package/src/deep.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Prettify } from './viem';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Copy the brand tag from the branded.ts file, can't import it to keep it hidden from library users
|
|
5
|
+
*/
|
|
6
|
+
const brandTag = '___tag___';
|
|
7
|
+
|
|
8
|
+
export type DeepRequire<T> = {
|
|
9
|
+
[P in keyof T]-?: DeepRequire<T[P]>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type DeepOptional<T> = T extends { [brandTag]: infer V }
|
|
13
|
+
? Prettify<
|
|
14
|
+
{ [brandTag]: V } & {
|
|
15
|
+
[P in keyof T]?: DeepOptional<T[P]>;
|
|
16
|
+
}
|
|
17
|
+
>
|
|
18
|
+
: {
|
|
19
|
+
[P in keyof T]?: DeepOptional<T[P]>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type DeepUnion<T, U> =
|
|
23
|
+
| {
|
|
24
|
+
[P in keyof T]: DeepUnion<T[P], U>;
|
|
25
|
+
}
|
|
26
|
+
| U;
|
package/src/identity.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export * from './advancedRecord';
|
|
2
|
+
export * from './branded';
|
|
3
|
+
export * from './constructor';
|
|
4
|
+
export * from './deep';
|
|
5
|
+
export * from './disallowedAny';
|
|
6
|
+
export * from './identity';
|
|
7
|
+
export * from './lastOf';
|
|
8
|
+
export * from './merge';
|
|
9
|
+
export * from './methodOf';
|
|
10
|
+
export * from './removeNever';
|
|
11
|
+
export * from './strings';
|
|
12
|
+
export * from './tuples';
|
|
13
|
+
export * from './valueOf';
|
|
14
|
+
export * from './viem';
|
|
15
|
+
export * from './withRequired';
|
package/src/lastOf.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
|
|
2
|
+
k: infer I,
|
|
3
|
+
) => void
|
|
4
|
+
? I
|
|
5
|
+
: never;
|
|
6
|
+
|
|
7
|
+
export type LastOf<T> =
|
|
8
|
+
UnionToIntersection<T extends any ? () => T : never> extends () => infer R ? R : never;
|
|
9
|
+
|
|
10
|
+
type Prepend<T, U extends any[]> = [T, ...U];
|
|
11
|
+
|
|
12
|
+
export type UnionToArray<T, U extends any[] = []> =
|
|
13
|
+
LastOf<T> extends never ? U : UnionToArray<Exclude<T, LastOf<T>>, Prepend<LastOf<T>, U>>;
|
|
14
|
+
|
|
15
|
+
export type RestOf<T> = Exclude<T, LastOf<T>>;
|
package/src/merge.ts
ADDED
package/src/methodOf.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { expectTypeOf, test } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import type { HexStringIsTrimmed, StringWithoutSuffix } from './strings';
|
|
4
|
+
|
|
5
|
+
test('StringWithoutSuffix', () => {
|
|
6
|
+
type A = StringWithoutSuffix<'hello', 'world'>;
|
|
7
|
+
expectTypeOf<A>().toBeString();
|
|
8
|
+
|
|
9
|
+
type B = StringWithoutSuffix<'hello', 'lo'>;
|
|
10
|
+
expectTypeOf<B>().toBeNever();
|
|
11
|
+
|
|
12
|
+
// Multiple suffixes test:
|
|
13
|
+
type C = StringWithoutSuffix<'hello', 'lo' | 'world'>;
|
|
14
|
+
expectTypeOf<C>().toBeNever();
|
|
15
|
+
|
|
16
|
+
type D = StringWithoutSuffix<'hello world', 'lo' | 'world'>;
|
|
17
|
+
expectTypeOf<D>().toBeNever();
|
|
18
|
+
|
|
19
|
+
type E = StringWithoutSuffix<'hello world!', 'lo' | 'world'>;
|
|
20
|
+
expectTypeOf<E>().toBeString();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('HexStringIsTrimmed', () => {
|
|
24
|
+
type A = HexStringIsTrimmed<'0x0'>;
|
|
25
|
+
expectTypeOf<A>().toExtend<true>();
|
|
26
|
+
|
|
27
|
+
type B = HexStringIsTrimmed<'0x00'>;
|
|
28
|
+
expectTypeOf<B>().toExtend<false>();
|
|
29
|
+
|
|
30
|
+
type C = HexStringIsTrimmed<'0x'>;
|
|
31
|
+
expectTypeOf<C>().toExtend<false>();
|
|
32
|
+
});
|
package/src/strings.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Branded } from './branded';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Guarantees that a string does not end with a suffix.
|
|
5
|
+
* You can remove multiple suffixes by using a union.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* type A = StringWithoutSuffix<'hello', 'world'>; // 'hello'
|
|
9
|
+
* type B = StringWithoutSuffix<'hello', 'lo'>; // never
|
|
10
|
+
* type C = StringWithoutSuffix<'hello world', 'lo' | 'world'>; // never
|
|
11
|
+
*/
|
|
12
|
+
export type StringWithoutSuffix<
|
|
13
|
+
T extends string,
|
|
14
|
+
Suffix extends string,
|
|
15
|
+
> = T extends `${infer _}${Suffix}` ? never : T;
|
|
16
|
+
|
|
17
|
+
export type HexString = `0x${string}`;
|
|
18
|
+
|
|
19
|
+
export declare const _NormalizedHexString: unique symbol;
|
|
20
|
+
|
|
21
|
+
export type NormalizedHexString = Branded<typeof _NormalizedHexString, 'NormalizedHexString'>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Guarantees that a hex string is trimmed.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* type A = HexStringIsTrimmed<'0x0'>; // true
|
|
28
|
+
* type B = HexStringIsTrimmed<'0x00'>; // false
|
|
29
|
+
* type C = HexStringIsTrimmed<'0x'>; // false
|
|
30
|
+
* type D = HexStringIsTrimmed<'0x100'>; // true
|
|
31
|
+
*/
|
|
32
|
+
export type HexStringIsTrimmed<T extends HexString> = T extends '0x0'
|
|
33
|
+
? true
|
|
34
|
+
: T extends '0x'
|
|
35
|
+
? false
|
|
36
|
+
: T extends `0x0${string}`
|
|
37
|
+
? false
|
|
38
|
+
: true;
|
|
39
|
+
|
|
40
|
+
export declare const _NumberString: unique symbol;
|
|
41
|
+
|
|
42
|
+
export type DecimalString = Branded<typeof _NumberString, 'DecimalString'>;
|
package/src/tuples.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { BrandedError } from './branded';
|
|
2
|
+
|
|
3
|
+
//check that the tuple T uses every type in the union K at least once
|
|
4
|
+
export type IsComplete<
|
|
5
|
+
T extends readonly (string | number | symbol)[],
|
|
6
|
+
K extends string | number | symbol,
|
|
7
|
+
> = Exclude<K, T[number]> extends never ? true : false;
|
|
8
|
+
|
|
9
|
+
export type TryGetDuplicate<
|
|
10
|
+
T extends readonly (string | number | symbol)[],
|
|
11
|
+
Seen extends (string | number | symbol)[] = [],
|
|
12
|
+
> = T extends [infer Head, ...infer Tail]
|
|
13
|
+
? Head extends Seen[number]
|
|
14
|
+
? Head
|
|
15
|
+
: Head extends string | number | symbol
|
|
16
|
+
? Tail extends readonly (string | number | symbol)[]
|
|
17
|
+
? TryGetDuplicate<Tail, [...Seen, Head]>
|
|
18
|
+
: false
|
|
19
|
+
: false
|
|
20
|
+
: false;
|
|
21
|
+
|
|
22
|
+
//check that the tuple T is a valid ordering of AllKeys, using IsComplete and TryGetDuplicate
|
|
23
|
+
//returns a branded error if there are missing keys or duplicates
|
|
24
|
+
export type AssertUniqueCompleteSet<
|
|
25
|
+
T extends readonly (string | number | symbol)[],
|
|
26
|
+
AllKeys extends string | number | symbol,
|
|
27
|
+
> =
|
|
28
|
+
IsComplete<T, AllKeys> extends false
|
|
29
|
+
? BrandedError<`Missing key: ${Exclude<AllKeys extends Symbol ? 'ERR' : AllKeys, T[number]>}`>
|
|
30
|
+
: TryGetDuplicate<T> extends false
|
|
31
|
+
? T
|
|
32
|
+
: BrandedError<`Duplicate key found: ${TryGetDuplicate<T>}`>;
|
|
33
|
+
|
|
34
|
+
type BuildTupleHelper<
|
|
35
|
+
Element,
|
|
36
|
+
Length extends number,
|
|
37
|
+
Rest extends Element[],
|
|
38
|
+
> = Rest['length'] extends Length
|
|
39
|
+
? readonly [...Rest] // Terminate with readonly array (aka tuple)
|
|
40
|
+
: BuildTupleHelper<Element, Length, [Element, ...Rest]>;
|
|
41
|
+
|
|
42
|
+
export type BuildTuple<Element, Length extends number> = number extends Length
|
|
43
|
+
? // Because `Length extends number` and `number extends Length`, then `Length` is not a specific finite number.
|
|
44
|
+
readonly Element[] // It's not fixed length.
|
|
45
|
+
: BuildTupleHelper<Element, Length, []>; // Otherwise it is a fixed length tuple.
|
|
46
|
+
|
|
47
|
+
export type TuplesToObject<T extends readonly (readonly [PropertyKey, any])[]> = {
|
|
48
|
+
[K in T[number] as K[0]]: K[1];
|
|
49
|
+
};
|
package/src/valueOf.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type IntersectionValueOf<T> = (
|
|
2
|
+
T[keyof T] extends infer U ? (U extends any ? (x: U) => void : never) : never
|
|
3
|
+
) extends (x: infer I) => void
|
|
4
|
+
? I
|
|
5
|
+
: never;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @description Creates a type that extracts the values of T.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ValueOf<{ a: string, b: number }>
|
|
12
|
+
* => string | number
|
|
13
|
+
*
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export type ValueOf<T> = T[keyof T];
|
package/src/viem.ts
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filters out all members of {@link T} that are not {@link P}
|
|
3
|
+
*
|
|
4
|
+
* @param T - Items to filter
|
|
5
|
+
* @param P - Type to filter out
|
|
6
|
+
* @returns Filtered items
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* type Result = Filter<['a', 'b', 'c'], 'b'>
|
|
10
|
+
* // ^? type Result = ['a', 'c']
|
|
11
|
+
*/
|
|
12
|
+
export type Filter<
|
|
13
|
+
T extends readonly unknown[],
|
|
14
|
+
P,
|
|
15
|
+
Acc extends readonly unknown[] = [],
|
|
16
|
+
> = T extends readonly [infer F, ...infer Rest extends readonly unknown[]]
|
|
17
|
+
? [F] extends [P]
|
|
18
|
+
? Filter<Rest, P, [...Acc, F]>
|
|
19
|
+
: Filter<Rest, P, Acc>
|
|
20
|
+
: readonly [...Acc];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @description Checks if {@link T} can be narrowed further than {@link U}
|
|
24
|
+
* @param T - Type to check
|
|
25
|
+
* @param U - Type to against
|
|
26
|
+
* @example
|
|
27
|
+
* type Result = IsNarrowable<'foo', string>
|
|
28
|
+
* // ^? true
|
|
29
|
+
*/
|
|
30
|
+
export type IsNarrowable<T, U> =
|
|
31
|
+
IsNever<(T extends U ? true : false) & (U extends T ? false : true)> extends true
|
|
32
|
+
? false
|
|
33
|
+
: true;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @description Checks if {@link T} is `never`
|
|
37
|
+
* @param T - Type to check
|
|
38
|
+
* @example
|
|
39
|
+
* type Result = IsNever<never>
|
|
40
|
+
* // ^? type Result = true
|
|
41
|
+
*/
|
|
42
|
+
export type IsNever<T> = [T] extends [never] ? true : false;
|
|
43
|
+
|
|
44
|
+
/** Removes `readonly` from all properties of an object. */
|
|
45
|
+
export type Mutable<type extends object> = {
|
|
46
|
+
-readonly [key in keyof type]: type[key];
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @description Evaluates boolean "or" condition for {@link T} properties.
|
|
51
|
+
* @param T - Type to check
|
|
52
|
+
*
|
|
53
|
+
* * @example
|
|
54
|
+
* type Result = Or<[false, true, false]>
|
|
55
|
+
* // ^? type Result = true
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* type Result = Or<[false, false, false]>
|
|
59
|
+
* // ^? type Result = false
|
|
60
|
+
*/
|
|
61
|
+
export type Or<T extends readonly unknown[]> = T extends readonly [infer Head, ...infer Tail]
|
|
62
|
+
? Head extends true
|
|
63
|
+
? true
|
|
64
|
+
: Or<Tail>
|
|
65
|
+
: false;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @description Checks if {@link T} is `undefined`
|
|
69
|
+
* @param T - Type to check
|
|
70
|
+
* @example
|
|
71
|
+
* type Result = IsUndefined<undefined>
|
|
72
|
+
* // ^? type Result = true
|
|
73
|
+
*/
|
|
74
|
+
export type IsUndefined<T> = [undefined] extends [T] ? true : false;
|
|
75
|
+
|
|
76
|
+
export type MaybePromise<T> = T | Promise<T>;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @description Makes attributes on the type T required if required is true.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* MaybeRequired<{ a: string, b?: number }, true>
|
|
83
|
+
* => { a: string, b: number }
|
|
84
|
+
*
|
|
85
|
+
* MaybeRequired<{ a: string, b?: number }, false>
|
|
86
|
+
* => { a: string, b?: number }
|
|
87
|
+
*/
|
|
88
|
+
export type MaybeRequired<T, required extends boolean> = required extends true
|
|
89
|
+
? ExactRequired<T>
|
|
90
|
+
: T;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @description Assigns the properties of U onto T.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* Assign<{ a: string, b: number }, { a: undefined, c: boolean }>
|
|
97
|
+
* => { a: undefined, b: number, c: boolean }
|
|
98
|
+
*/
|
|
99
|
+
export type Assign<T, U> = Assign_<T, U> & U;
|
|
100
|
+
type Assign_<T, U> = {
|
|
101
|
+
[K in keyof T as K extends keyof U ? (U[K] extends void ? never : K) : K]: K extends keyof U
|
|
102
|
+
? U[K]
|
|
103
|
+
: T[K];
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// TODO: Remove when peer dep `typescript@>=4.5` (NoInfer is native)
|
|
107
|
+
export type NoInfer<type> = [type][type extends any ? 0 : never];
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @description Constructs a type by excluding `undefined` from `T`.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* NoUndefined<string | undefined>
|
|
114
|
+
* => string
|
|
115
|
+
*
|
|
116
|
+
* @internal
|
|
117
|
+
*/
|
|
118
|
+
export type NoUndefined<T> = T extends undefined ? never : T;
|
|
119
|
+
|
|
120
|
+
/** Strict version of built-in Omit type */
|
|
121
|
+
export type Omit<type, keys extends keyof type> = Pick<type, Exclude<keyof type, keys>>;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @description Creates a type that is a partial of T, but with the required keys K.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* PartialBy<{ a: string, b: number }, 'a'>
|
|
128
|
+
* => { a?: string, b: number }
|
|
129
|
+
*/
|
|
130
|
+
export type PartialBy<T, K extends keyof T> = Omit<T, K> & ExactPartial<Pick<T, K>>;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @description Combines members of an intersection into a readable type.
|
|
134
|
+
*
|
|
135
|
+
* @see {@link https://twitter.com/mattpocockuk/status/1622730173446557697?s=20&t=NdpAcmEFXY01xkqU3KO0Mg}
|
|
136
|
+
* @example
|
|
137
|
+
* Prettify<{ a: string } & { b: string } & { c: number, d: bigint }>
|
|
138
|
+
* => { a: string, b: string, c: number, d: bigint }
|
|
139
|
+
*/
|
|
140
|
+
export type Prettify<T> = {
|
|
141
|
+
[K in keyof T]: T[K];
|
|
142
|
+
} & {};
|
|
143
|
+
|
|
144
|
+
/** @internal */
|
|
145
|
+
export type Evaluate<type> = {
|
|
146
|
+
[key in keyof type]: type[key];
|
|
147
|
+
} & {};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @description Creates a type that is T with the required keys K.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* RequiredBy<{ a?: string, b: number }, 'a'>
|
|
154
|
+
* => { a: string, b: number }
|
|
155
|
+
*/
|
|
156
|
+
export type RequiredBy<T, K extends keyof T> = Omit<T, K> & ExactRequired<Pick<T, K>>;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* @description Returns truthy if `array` contains `value`.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* Some<[1, 2, 3], 2>
|
|
163
|
+
* => true
|
|
164
|
+
*/
|
|
165
|
+
export type Some<array extends readonly unknown[], value> = array extends readonly [
|
|
166
|
+
value,
|
|
167
|
+
...unknown[],
|
|
168
|
+
]
|
|
169
|
+
? true
|
|
170
|
+
: array extends readonly [unknown, ...infer rest]
|
|
171
|
+
? Some<rest, value>
|
|
172
|
+
: false;
|
|
173
|
+
|
|
174
|
+
export type UnionToTuple<
|
|
175
|
+
union,
|
|
176
|
+
///
|
|
177
|
+
last = LastInUnion<union>,
|
|
178
|
+
> = [union] extends [never] ? [] : [...UnionToTuple<Exclude<union, last>>, last];
|
|
179
|
+
type LastInUnion<U> =
|
|
180
|
+
UnionToIntersection<U extends unknown ? (x: U) => 0 : never> extends (x: infer l) => 0
|
|
181
|
+
? l
|
|
182
|
+
: never;
|
|
183
|
+
type UnionToIntersection<union> = (union extends unknown ? (arg: union) => 0 : never) extends (
|
|
184
|
+
arg: infer i,
|
|
185
|
+
) => 0
|
|
186
|
+
? i
|
|
187
|
+
: never;
|
|
188
|
+
|
|
189
|
+
export type IsUnion<
|
|
190
|
+
union,
|
|
191
|
+
///
|
|
192
|
+
union2 = union,
|
|
193
|
+
> = union extends union2 ? ([union2] extends [union] ? false : true) : never;
|
|
194
|
+
|
|
195
|
+
export type MaybePartial<type, enabled extends boolean | undefined> = enabled extends true
|
|
196
|
+
? Prettify<ExactPartial<type>>
|
|
197
|
+
: type;
|
|
198
|
+
|
|
199
|
+
export type ExactPartial<type> = {
|
|
200
|
+
[key in keyof type]?: type[key] | undefined;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
export type ExactRequired<type> = {
|
|
204
|
+
[P in keyof type]-?: Exclude<type[P], undefined>;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
export type OneOf<
|
|
208
|
+
union extends object,
|
|
209
|
+
fallback extends object | undefined = undefined,
|
|
210
|
+
///
|
|
211
|
+
keys extends KeyofUnion<union> = KeyofUnion<union>,
|
|
212
|
+
> = union extends infer item
|
|
213
|
+
? Prettify<
|
|
214
|
+
item & {
|
|
215
|
+
[key in Exclude<keys, keyof item>]?: fallback extends object
|
|
216
|
+
? key extends keyof fallback
|
|
217
|
+
? fallback[key]
|
|
218
|
+
: undefined
|
|
219
|
+
: undefined;
|
|
220
|
+
}
|
|
221
|
+
>
|
|
222
|
+
: never;
|
|
223
|
+
type KeyofUnion<type> = type extends type ? keyof type : never;
|
|
224
|
+
|
|
225
|
+
///////////////////////////////////////////////////////////////////////////
|
|
226
|
+
// Loose types
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Loose version of {@link Omit}
|
|
230
|
+
* @internal
|
|
231
|
+
*/
|
|
232
|
+
export type LooseOmit<type, keys extends string> = Pick<type, Exclude<keyof type, keys>>;
|
|
233
|
+
|
|
234
|
+
///////////////////////////////////////////////////////////////////////////
|
|
235
|
+
// Union types
|
|
236
|
+
|
|
237
|
+
export type UnionEvaluate<type> = type extends object ? Prettify<type> : type;
|
|
238
|
+
|
|
239
|
+
export type UnionLooseOmit<type, keys extends string> = type extends any
|
|
240
|
+
? LooseOmit<type, keys>
|
|
241
|
+
: never;
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* @description Construct a type with the properties of union type T except for those in type K.
|
|
245
|
+
* @example
|
|
246
|
+
* type Result = UnionOmit<{ a: string, b: number } | { a: string, b: undefined, c: number }, 'a'>
|
|
247
|
+
* => { b: number } | { b: undefined, c: number }
|
|
248
|
+
*/
|
|
249
|
+
export type UnionOmit<type, keys extends keyof type> = type extends any ? Omit<type, keys> : never;
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* @description Construct a type with the properties of union type T except for those in type K.
|
|
253
|
+
* @example
|
|
254
|
+
* type Result = UnionOmit<{ a: string, b: number } | { a: string, b: undefined, c: number }, 'a'>
|
|
255
|
+
* => { b: number } | { b: undefined, c: number }
|
|
256
|
+
*/
|
|
257
|
+
export type UnionPick<type, keys extends keyof type> = type extends any ? Pick<type, keys> : never;
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* @description Creates a type that is a partial of T, but with the required keys K.
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* PartialBy<{ a: string, b: number } | { a: string, b: undefined, c: number }, 'a'>
|
|
264
|
+
* => { a?: string, b: number } | { a?: string, b: undefined, c: number }
|
|
265
|
+
*/
|
|
266
|
+
export type UnionPartialBy<T, K extends keyof T> = T extends any ? PartialBy<T, K> : never;
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* @description Creates a type that is T with the required keys K.
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* RequiredBy<{ a?: string, b: number } | { a?: string, c?: number }, 'a'>
|
|
273
|
+
* => { a: string, b: number } | { a: string, c?: number }
|
|
274
|
+
*/
|
|
275
|
+
export type UnionRequiredBy<T, K extends keyof T> = T extends any ? RequiredBy<T, K> : never;
|