Skip to content

Commit 9523aa0

Browse files
author
Amine Ben hammou
committed
Release 2.0.0-alpha.2
1 parent c92ab31 commit 9523aa0

12 files changed

+143
-8
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ yarn add --dev just-types
3232

3333
- [common](https://github.com/webNeat/just-types/blob/main/docs/common.md)
3434
- [Mutable](https://github.com/webNeat/just-types/blob/main/docs/common.md#mutable)
35+
- [Normalize](https://github.com/webNeat/just-types/blob/main/docs/common.md#normalize)
3536

3637
- [number](https://github.com/webNeat/just-types/blob/main/docs/number.md)
3738
- [Decrement](https://github.com/webNeat/just-types/blob/main/docs/number.md#decrement)
@@ -46,6 +47,10 @@ yarn add --dev just-types
4647
- [FieldPath](https://github.com/webNeat/just-types/blob/main/docs/object.md#fieldpath)
4748
- [GetField](https://github.com/webNeat/just-types/blob/main/docs/object.md#getfield)
4849
- [Merge](https://github.com/webNeat/just-types/blob/main/docs/object.md#merge)
50+
- [PartialKeys](https://github.com/webNeat/just-types/blob/main/docs/object.md#partialkeys)
51+
- [PartialValues](https://github.com/webNeat/just-types/blob/main/docs/object.md#partialvalues)
52+
- [RequiredKeys](https://github.com/webNeat/just-types/blob/main/docs/object.md#requiredkeys)
53+
- [RequiredValues](https://github.com/webNeat/just-types/blob/main/docs/object.md#requiredvalues)
4954

5055
- [string](https://github.com/webNeat/just-types/blob/main/docs/string.md)
5156
- [Split](https://github.com/webNeat/just-types/blob/main/docs/string.md#split)

docs/common.md

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Table Of Contents
44

55
- [Mutable](#mutable)
6+
- [Normalize](#normalize)
67

78
## Mutable
89

@@ -15,4 +16,15 @@ Mutable<readonly []> //=> []
1516
Mutable<readonly ['a']> //=> ['a']
1617
Mutable<readonly ['a', 'b']> //=> ['a', 'b']
1718
Mutable<{readonly a: string, readonly b: number}> //=> {a: string, b: number}
19+
```
20+
21+
## Normalize
22+
Flattens the type to allow better auto-complete in editors.
23+
```ts
24+
import {Normalize} from 'just-types/common'
25+
// or
26+
import {common} from 'just-types' // and use common.Normalize
27+
28+
Normalize<string> //=> string
29+
Normalize<{a: string} & {b: number}> //=> {a: string, b: number}
1830
```

docs/object.md

+52
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
- [FieldPath](#fieldpath)
1010
- [GetField](#getfield)
1111
- [Merge](#merge)
12+
- [PartialKeys](#partialkeys)
13+
- [PartialValues](#partialvalues)
14+
- [RequiredKeys](#requiredkeys)
15+
- [RequiredValues](#requiredvalues)
1216

1317
## ExcludeKeys
1418

@@ -114,4 +118,52 @@ Merge<
114118
// a: {foo: number; bar: {baz: bigint}}
115119
// c: {c1: number; c2: number; d: {e: {f: null; h: number}; g: null}}
116120
// }
121+
```
122+
123+
## PartialKeys
124+
125+
```ts
126+
import {PartialKeys} from 'just-types/object'
127+
// or
128+
import {object} from 'just-types' // and use object.PartialKeys
129+
130+
PartialKeys<{a: string, b: number, c: boolean}, 'a' | 'c'> //=> {a?: string, b: number, c?: boolean}
131+
PartialKeys<{a: string, b: number, c: boolean}, string> //=> {a?: string, b?: number, c?: boolean}
132+
PartialKeys<{a?: string, b?: number, c: boolean}, 'a' | 'd'> //=> {a?: string, b?: number, c: boolean}
133+
```
134+
135+
## PartialValues
136+
137+
```ts
138+
import {PartialValues} from 'just-types/object'
139+
// or
140+
import {object} from 'just-types' // and use object.PartialValues
141+
142+
PartialValues<{a: string, b: number, c: boolean}, string | boolean> //=> {a?: string, b: number, c?: boolean}
143+
PartialValues<{a: 'Hello', b: string, c: boolean}, string> //=> {a?: 'Hello', b?: string, c: boolean}
144+
PartialValues<{a?: string, b?: number, c: boolean}, string> //=> {a?: string, b?: number, c: boolean}
145+
```
146+
147+
## RequiredKeys
148+
149+
```ts
150+
import {RequiredKeys} from 'just-types/object'
151+
// or
152+
import {object} from 'just-types' // and use object.RequiredKeys
153+
154+
RequiredKeys<{a?: string, b?: number, c: boolean}, 'a' | 'c'> //=> {a: string, b?: number, c: boolean}
155+
RequiredKeys<{a?: string, b?: number, c: boolean}, string> //=> {a: string, b: number, c: boolean}
156+
RequiredKeys<{a?: string, b?: number, c: boolean}, 'a' | 'd'> //=> {a: string, b?: number, c: boolean}
157+
```
158+
159+
## RequiredValues
160+
161+
```ts
162+
import {RequiredValues} from 'just-types/object'
163+
// or
164+
import {object} from 'just-types' // and use object.RequiredValues
165+
166+
RequiredValues<{a?: string, b?: number, c?: boolean}, string | boolean> //=> {a: string, b?: number, c: boolean}
167+
RequiredValues<{a?: 'Hello', b?: string, c?: boolean}, string> //=> {a: 'Hello', b: string, c?: boolean}
168+
RequiredValues<{a: string, b: number, c?: boolean}, string> //=> {a: string, b: number, c?: boolean}
117169
```

package.json

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"author": "Amine Ben hammou",
44
"description": "A collection of handy Typescript types.",
55
"license": "MIT",
6-
"version": "2.0.0-alpha.1",
6+
"version": "2.0.0-alpha.2",
77
"repository": "webNeat/just-types",
88
"homepage": "https://github.com/webNeat/just-types#just-types",
99
"main": "dist/index.js",
@@ -19,7 +19,7 @@
1919
"./string": "./dist/string/index.js",
2020
"./test": "./dist/test.js",
2121
"./tuple": "./dist/tuple/index.js"
22-
},
22+
},
2323
"engines": {
2424
"node": ">=16"
2525
},
@@ -29,14 +29,16 @@
2929
"build:docs": "ts-node-esm build.mts",
3030
"build": "yarn build:ts && yarn build:docs"
3131
},
32+
"dependencies": {
33+
"tslib": "^2.5.0",
34+
"@types/node": "^18.15.11",
35+
"typescript": "^5.0.3"
36+
},
3237
"devDependencies": {
3338
"@types/fs-extra": "^11.0.1",
34-
"@types/node": "^18.15.11",
3539
"expect-type": "^0.15.0",
3640
"fs-extra": "^11.1.1",
3741
"prettier": "^2.8.7",
38-
"ts-node": "^10.9.1",
39-
"tslib": "^2.5.0",
40-
"typescript": "^5.0.3"
42+
"ts-node": "^10.9.1"
4143
}
4244
}

src/common/Normalize.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Equal, Is } from "../test"
2+
3+
/**
4+
* Flattens the type to allow better auto-complete in editors.
5+
*/
6+
export type Normalize<T> = {
7+
[K in keyof T]: T[K]
8+
}
9+
10+
type Tests = [
11+
Is<Equal<Normalize<string>, string>>,
12+
Is<Equal<Normalize<{a: string} & {b: number}>, {a: string, b: number}>>,
13+
]

src/common/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from './Mutable'
1+
export * from './Mutable'
2+
export * from './Normalize'

src/object/PartialKeys.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Normalize } from "../common"
2+
import { Equal, Is } from "../test"
3+
4+
export type PartialKeys<T, U> = Normalize<Omit<T, Extract<keyof T, U>> & Partial<Pick<T, Extract<keyof T, U>>>>
5+
6+
type Tests = [
7+
Is<Equal<PartialKeys<{a: string, b: number, c: boolean}, 'a' | 'c'>, {a?: string, b: number, c?: boolean}>>,
8+
Is<Equal<PartialKeys<{a: string, b: number, c: boolean}, string>, {a?: string, b?: number, c?: boolean}>>,
9+
Is<Equal<PartialKeys<{a?: string, b?: number, c: boolean}, 'a' | 'd'>, {a?: string, b?: number, c: boolean}>>,
10+
]

src/object/PartialValues.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Normalize } from "../common"
2+
import { Equal, Is } from "../test"
3+
4+
export type PartialValues<T, U> = Normalize<
5+
{[K in keyof T as Extract<T[K], U> extends never ? K : never]: T[K]} &
6+
{[K in keyof T as Extract<T[K], U> extends never ? never : K]?: T[K]}
7+
>
8+
9+
type Tests = [
10+
Is<Equal<PartialValues<{a: string, b: number, c: boolean}, string | boolean>, {a?: string, b: number, c?: boolean}>>,
11+
Is<Equal<PartialValues<{a: 'Hello', b: string, c: boolean}, string>, {a?: 'Hello', b?: string, c: boolean}>>,
12+
Is<Equal<PartialValues<{a?: string, b?: number, c: boolean}, string>, {a?: string, b?: number, c: boolean}>>,
13+
]

src/object/RequiredKeys.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Normalize } from "../common"
2+
import { Equal, Is } from "../test"
3+
4+
export type RequiredKeys<T, U> = Normalize<Omit<T, Extract<keyof T, U>> & Required<Pick<T, Extract<keyof T, U>>>>
5+
6+
type Tests = [
7+
Is<Equal<RequiredKeys<{a?: string, b?: number, c: boolean}, 'a' | 'c'>, {a: string, b?: number, c: boolean}>>,
8+
Is<Equal<RequiredKeys<{a?: string, b?: number, c: boolean}, string>, {a: string, b: number, c: boolean}>>,
9+
Is<Equal<RequiredKeys<{a?: string, b?: number, c: boolean}, 'a' | 'd'>, {a: string, b?: number, c: boolean}>>,
10+
]

src/object/RequiredValues.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Normalize } from "../common"
2+
import { Equal, Is } from "../test"
3+
4+
export type RequiredValues<T, U> = Normalize<
5+
{[K in keyof T as Extract<T[K], U> extends never ? K : never]: T[K]} &
6+
{[K in keyof T as Extract<T[K], U> extends never ? never : K]-?: T[K]}
7+
>
8+
9+
type Tests = [
10+
Is<Equal<RequiredValues<{a?: string, b?: number, c?: boolean}, string | boolean>, {a: string, b?: number, c: boolean}>>,
11+
Is<Equal<RequiredValues<{a?: 'Hello', b?: string, c?: boolean}, string>, {a: 'Hello', b: string, c?: boolean}>>,
12+
Is<Equal<RequiredValues<{a: string, b: number, c?: boolean}, string>, {a: string, b: number, c?: boolean}>>,
13+
]

src/object/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ export * from './ExtractValues'
55
export * from './FieldPath'
66
export * from './GetField'
77
export * from './Merge'
8+
export * from './PartialKeys'
9+
export * from './PartialValues'
10+
export * from './RequiredKeys'
11+
export * from './RequiredValues'

src/tuple/Flatten.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type Flatten<T extends any[], Depth extends ZeroTo100 = 1> =
66
[Depth, T] extends [0, any] | [any, []] ? T :
77
T extends [infer Head, ...infer Tail]
88
? Head extends any[]
9-
// @ts-expect-error
9+
// @ts-ignore
1010
? [...Flatten<Head, Decrement<Depth>>, ...Flatten<Tail, Depth>]
1111
: [Head, ...Flatten<Tail, Depth>]
1212
: T

0 commit comments

Comments
 (0)