Skip to content

Commit

Permalink
feat: generic hkts, platform keywords (#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalbdivad authored Jul 24, 2024
1 parent 1820e86 commit 29e4aa8
Show file tree
Hide file tree
Showing 101 changed files with 2,000 additions and 975 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@
},
"debug.javascript.terminalOptions": {
"skipFiles": ["<node_internals>/**", "**/node_modules/**"]
},
"editor.quickSuggestions": {
"strings": "on"
}
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2023 ArkType
Copyright 2024 ArkType

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
4 changes: 4 additions & 0 deletions ark/attest/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @ark/attest

## 0.9.2

Fix a bug preventing consecutive benchmark runs from populating snapshots inline

## 0.8.2

### Patch Changes
Expand Down
13 changes: 11 additions & 2 deletions ark/attest/bench/bench.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { caller, type SourcePosition } from "@ark/fs"
import { caller, rmRf, type SourcePosition } from "@ark/fs"
import { performance } from "node:perf_hooks"
import {
ensureCacheDirs,
Expand All @@ -22,6 +22,8 @@ export type StatName = keyof typeof stats

export type TimeAssertionName = StatName | "mark"

let benchHasRun = false

export const bench = <Fn extends BenchableFunction>(
name: string,
fn: Fn,
Expand All @@ -34,8 +36,15 @@ export const bench = <Fn extends BenchableFunction>(
fn.constructor.name === "AsyncFunction",
options
)

if (!benchHasRun) {
rmRf(ctx.cfg.cacheDir)
ensureCacheDirs()
benchHasRun = true
}

ctx.benchCallPosition = caller()
ensureCacheDirs()

if (
typeof ctx.cfg.filter === "string" &&
!qualifiedPath.includes(ctx.cfg.filter)
Expand Down
4 changes: 2 additions & 2 deletions ark/attest/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ensureDir, fromCwd } from "@ark/fs"
import {
arrayFrom,
isArray,
liftArray,
tryParseNumber,
type autocomplete
} from "@ark/util"
Expand Down Expand Up @@ -135,7 +135,7 @@ const parseTsVersions = (aliases: TsVersionAliases): TsVersionData[] => {
const versions = findAttestTypeScriptVersions()
if (aliases === "*") return versions

return arrayFrom(aliases).map(alias => {
return liftArray(aliases).map(alias => {
const matching = versions.find(v => v.alias === alias)
if (!matching) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion ark/attest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ark/attest",
"version": "0.9.1",
"version": "0.9.3",
"author": {
"name": "David Blass",
"email": "david@arktype.io",
Expand Down
3 changes: 3 additions & 0 deletions ark/dark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
}
],
"configurationDefaults": {
"editor.quickSuggestions": {
"strings": "on"
},
"errorLens.followCursor": "closestProblem",
"errorLens.delay": 0,
"errorLens.editorHoverPartsEnabled": {
Expand Down
24 changes: 22 additions & 2 deletions ark/docs/src/content/docs/intro/setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,28 @@ You'll also need...
- [`skipLibCheck`](https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes) (strongly recommended, see [FAQ](/reference/faq#why-do-i-see-type-errors-in-an-arktype-package-in-node_modules))
- [`exactOptionalPropertyTypes`](https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes) (recommended)

## Extension (optional)
## VSCode

If you're using VSCode, we recommend installing [ArkDark](https://marketplace.visualstudio.com/items?itemName=arktypeio.arkdark), an extension we built to provide the embedded syntax highlighting you'll see throughout these docs.
### Settings

To take advantage of all of ArkType's autocomplete capabilities, you'll need to add the following to your workspace settings at `.vscode/settings.json`:

```jsonc
{
// other settings...
// allow autocomplete for ArkType expressions like "string | num"
"editor.quickSuggestions": {
"strings": "on"
}
}
```

### Extension (optional)

[ArkDark](https://marketplace.visualstudio.com/items?itemName=arktypeio.arkdark) provides the embedded syntax highlighting you'll see throughout the docs.

Even without it, your definitions will feel like a natural extension of the language. With it, you'll forget the boundary altogether.

## Other editors

If you're using a different editor, we'd love [help adding support](https://github.com/arktypeio/arktype/issues/989). In the meantime, don't worry- ArkType still offers best-in-class DX anywhere TypeScript is supported.
55 changes: 54 additions & 1 deletion ark/docs/src/content/docs/reference/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,60 @@ import { type } from "arktype"
const stringRecord = type("Record<string, string>")
```

Other common utils like `Pick` and `Omit` to follow in the an upcoming release.
In addition to `Record`, the following generics from TS are now available in ArkType:

- **Pick**
- **Omit**
- **Extract**
- **Exclude**

These can be instantiated in one of three ways:

### Syntactic Definition

```ts
import { type } from "arktype"

const one = type("Extract<0 | 1, 1>")
```

### Chained Definition

```ts
import { type } from "arktype"

const user = type({
name: "string",
"age?": "number",
isAdmin: "boolean"
})

// hover me!
const basicUser = user.pick("name", "age")
```

### Invoked Definition

```ts
import { ark } from "arktype"

const unfalse = ark.Exclude("boolean", "false")
```

### Generic HKTs

Our new generics have been built using a new method for integrating arbitrary external types as native ArkType generics! This opens up tons of possibilities for external integrations that would otherwise not be possible, but we're still finalizing the API. As a preview, here's what the implementation of `Exclude` looks like internally:

```ts
// @noErrors
class ArkExclude extends generic("T", "U")(args => args.T.exclude(args.U)) {
declare hkt: (
args: conform<this[Hkt.args], [unknown, unknown]>
) => Exclude<(typeof args)[0], (typeof args)[1]>
}
```

More to come on this as the API is finalized!

Recursive and cyclic generics are also currently unavailable and will be added soon.

Expand Down
2 changes: 1 addition & 1 deletion ark/fs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ark/fs",
"version": "0.1.0",
"version": "0.1.1",
"author": {
"name": "David Blass",
"email": "david@arktype.io",
Expand Down
22 changes: 10 additions & 12 deletions ark/repo/scratch.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { scope, type } from "arktype"
import { ark, scope, type } from "arktype"

const nonEmpty = type("<arr extends unknown[]>", "arr > 0")
const t = type({
foo: "string | number",
bar: ["number | string | boolean"]
})

const m = nonEmpty("number[]")
const o = type("string")

const threeSixtyNoModule = scope({
three: "3",
sixty: "60",
no: "'no'"
}).export()
ark.Record

const types = scope({
...threeSixtyNoModule,
threeSixtyNo: "three|sixty|no"
}).export()
// ("string", {
// bar: "number | string"
// })
121 changes: 0 additions & 121 deletions ark/repo/scratch/hkt.ts

This file was deleted.

1 change: 1 addition & 0 deletions ark/schema/__tests__/errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { attest, contextualize } from "@ark/attest"
import { configure, schema } from "@ark/schema"
import { $ark } from "@ark/util"
import { schemaScope } from "../scope.js"

contextualize(() => {
Expand Down
6 changes: 3 additions & 3 deletions ark/schema/__tests__/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { attest, contextualize } from "@ark/attest"
import { type Root, schema } from "@ark/schema"
import { type SchemaRoot, schema } from "@ark/schema"

contextualize(() => {
it("single constraint", () => {
const t = schema({ domain: "string", pattern: ".*" })
attest<Root<string>>(t)
attest<SchemaRoot<string>>(t)
attest(t.json).snap({ domain: "string", pattern: [".*"] })
})

Expand All @@ -19,7 +19,7 @@ contextualize(() => {
divisor: 5
})
const result = l.and(r)
attest<Root<number>>(result)
attest<SchemaRoot<number>>(result)
attest(result.json).snap({
domain: "number",
divisor: 15,
Expand Down
2 changes: 1 addition & 1 deletion ark/schema/__tests__/unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { attest, contextualize } from "@ark/attest"
import { assertNodeKind, schema } from "@ark/schema"
import { registeredReference } from "@ark/util"
import { registeredReference } from "../shared/registry.js"

contextualize(() => {
it("string allows", () => {
Expand Down
Loading

0 comments on commit 29e4aa8

Please sign in to comment.