Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify util types, add attest unit/bench examples #210

Merged
merged 8 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ node_modules
tsconfig*.tsbuildinfo
bench
playgrounds/performance/out
# temporary type-testing cache
.attest

# local env files
.env
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"version:update": "bun .scripts/updateVersion.ts"
},
"devDependencies": {
"@arktype/attest": "0.0.4",
"@biomejs/biome": "1.2.2",
"@changesets/changelog-github": "^0.4.8",
"@changesets/cli": "^2.26.0",
Expand Down
11 changes: 4 additions & 7 deletions packages/abitype/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"build:esm+types": "tsc --project tsconfig.build.json --module es2020 --outDir ./dist/esm --declaration --declarationMap --declarationDir ./dist/types && echo > ./dist/esm/package.json '{\"type\":\"module\",\"sideEffects\":false}'",
"clean": "rm -rf dist tsconfig.tsbuildinfo abis zod",
"test:build": "publint --strict",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"typeperf": "tsc --noEmit --extendedDiagnostics --composite false --incremental false"
},
"files": [
"dist",
Expand Down Expand Up @@ -48,12 +49,8 @@
},
"typesVersions": {
"*": {
"abis": [
"./dist/types/exports/abis.d.ts"
],
"zod": [
"./dist/types/exports/zod.d.ts"
]
"abis": ["./dist/types/exports/abis.d.ts"],
"zod": ["./dist/types/exports/zod.d.ts"]
}
},
"peerDependencies": {
Expand Down
12 changes: 12 additions & 0 deletions packages/abitype/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ type KeyofUnion<T> = T extends T ? keyof T : never
*/
export type Pretty<T> = { [K in keyof T]: T[K] } & unknown

/**
* Check that a type is a subtype of another.
*
* Useful for ensuring more complex types conform to a base pattern, e.g. by
* defining a set of keys.
*
* @param Base - The type that T must extend.
* @param T - The type to check.
* @returns T
*/
export type Satisfy<Base, T extends Base> = T

/**
* Creates range between two positive numbers using [tail recursion](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#tail-recursion-elimination-on-conditional-types).
*
Expand Down
34 changes: 34 additions & 0 deletions packages/abitype/src/utils.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { bench } from '@arktype/attest'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives you an idea of the general structure for a set of attest benchmarks.

It will allow you to chain .types() off the bench call. You just run the file as normal using something like ts-node, tsx etc. (hopefully bun but they may still have some issues with source maps).

When it executes for the first time or with no snapped value, it will populate the inline snapshot with a summary of the instantiation count.

Subsequent runs will summarize the delta between the current result and the snapped one.

If you want to configure it to fail above a certain threshold e.g. for CI, there are two settings you can pass through the CLI:

--benchPercentThreshold (defaults to 20)
--benchErrorOnThresholdExceeded (defaults to false)

If you set --benchErrorOnThresholdExceeded to true like tsx src/utils.bench.ts ----benchErrorOnThresholdExceeded, the process should return a non-zero exit code if any of the benchmarks have increased by an amount more than --benchPercentThreshold.

Note those flags haven't really been tested extensively, but figured I'd mentioned then since @tmm expressed an interest in CI integration.

import type { TypedDataToPrimitiveTypes } from './utils.js'

bench('recursive', () => {
const types3 = {
Foo: [{ name: 'bar', type: 'Bar[]' }],
Bar: [{ name: 'foo', type: 'Foo[]' }],
} as const
return {} as TypedDataToPrimitiveTypes<typeof types3>
}).types([12, 'instantiations'])

bench('deep', () => {
const types = {
Contributor: [
{ name: 'name', type: 'string' },
{ name: 'address', type: 'address' },
],
Website: [
{ name: 'domain', type: 'string' },
{ name: 'webmaster', type: 'Contributor' },
],
Project: [
{ name: 'name', type: 'string' },
{ name: 'contributors', type: 'Contributor[2]' },
{ name: 'website', type: 'Website' },
],
Organization: [
{ name: 'name', type: 'string' },
{ name: 'projects', type: 'Project[]' },
{ name: 'website', type: 'Website' },
],
} as const
return {} as TypedDataToPrimitiveTypes<typeof types>
}).types([44, 'instantiations'])
20 changes: 20 additions & 0 deletions packages/abitype/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { attest } from '@arktype/attest'
import { test } from 'vitest'
import type { TypedDataToPrimitiveTypes } from './utils.js'

test('self-referencing', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax closely parallels the type assertions vitest allows, with the benefit of being able to run them conditionally based on your environment since the assertion is still executed as normal at runtime.

Another significant benefit is that you can write these assertions alongside or in combination with your value assertions, so you don't need to have separate .d.ts tests.

This can be especially helpful when testing types and values that parallel one another, e.g. via APIs like .throwsAndHasTypeError

It also allows type/value snapshotting, object literal snapshots etc. (see the admittedly underdocumented README).

type Result = TypedDataToPrimitiveTypes<{
Name: [{ name: 'first'; type: 'Name' }, { name: 'last'; type: 'string' }]
}>
attest<
{
Name: {
first: [
"Error: Cannot convert self-referencing struct 'Name' to primitive type.",
]
last: string
}
},
Result
>()
})
Loading