Skip to content

Commit a4a0f8d

Browse files
committed
refactor: improve prettify type performance and benchmark types
1 parent a23382a commit a4a0f8d

File tree

6 files changed

+53
-8
lines changed

6 files changed

+53
-8
lines changed

benchmarks/deep_partial.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { bench } from '@ark/attest'
2+
import { type DeepPartial } from '../src/base.ts'
3+
4+
export type BaseLine = DeepPartial<{}>
5+
6+
bench('prettify', () => {
7+
type User = DeepPartial<{
8+
id: number
9+
scores: number[]
10+
profile: {
11+
twitterHandle?: string
12+
githubHandle?: string
13+
}
14+
}>
15+
16+
return {} as User
17+
}).types([1, 'instantiations'])

benchmarks/prettify.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { bench } from '@ark/attest'
2+
import { type Prettify } from '../src/base.ts'
3+
4+
export type BaseLine = Prettify<{}>
5+
6+
bench('prettify', () => {
7+
type JSONObject<V> = Prettify<{
8+
[K in keyof V]: V[K]
9+
}>
10+
type JSONArray<V> = Prettify<Array<V>>
11+
12+
type User = JSONObject<{
13+
id: number
14+
scores: JSONArray<number>
15+
profile: JSONObject<{
16+
twitterHandle?: string
17+
githubHandle?: string
18+
}>
19+
}>
20+
21+
return {} as User
22+
}).types([1, 'instantiations'])

benchmarks/route_params.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { bench } from '@ark/attest'
2+
import { type InferRouteParams } from '../src/route.ts'
3+
4+
export type BaseLine = InferRouteParams<'/'>
5+
6+
bench('prettify', () => {
7+
type ViewUser = InferRouteParams<'/users/:id/:name/:slug?'>
8+
return {} as ViewUser
9+
}).types([1, 'instantiations'])

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@adonisjs/eslint-config": "^3.0.0-next.1",
3131
"@adonisjs/prettier-config": "^1.4.5",
3232
"@adonisjs/tsconfig": "^2.0.0-next.0",
33+
"@ark/attest": "^0.55.0",
3334
"@japa/assert": "^4.1.1",
3435
"@japa/expect-type": "^2.0.3",
3536
"@japa/file-system": "^2.3.2",

src/base.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@
1010
/*
1111
* Pretty print complex types
1212
*/
13-
export type Prettify<T> = T extends Function
14-
? T
15-
: Extract<
16-
{
17-
[Key in keyof T]: T[Key]
18-
},
19-
T
20-
>
13+
export type Prettify<T> = {
14+
[K in keyof T]: T[K]
15+
} & {}
2116

2217
/**
2318
* Primitive values

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "@adonisjs/tsconfig/tsconfig.package.json",
33
"compilerOptions": {
44
"rootDir": "./",
5+
"moduleResolution": "nodenext",
56
"outDir": "./build"
67
}
78
}

0 commit comments

Comments
 (0)