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

fix: allow instanceof abstract classes, avoid inferring some function props as morphs #877

Merged
merged 2 commits into from
Nov 13, 2023
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: 1 addition & 1 deletion .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Install dependencies and perform setup for https://github.com/arkty

inputs:
node:
default: lts/*
default: 18

runs:
using: composite
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ jobs:
timeout-minutes: 20
strategy:
matrix:
node: [lts/*]
# https://github.com/arktypeio/arktype/issues/738
node: [16, 18]
os: [windows-latest, macos-latest]
include:
- os: ubuntu-latest
node: lts/-1
# https://github.com/arktypeio/arktype/issues/738
# - os: ubuntu-latest
# node: latest
node: 16
fail-fast: false

runs-on: ${{ matrix.os }}
Expand Down
5 changes: 5 additions & 0 deletions dev/configs/.changeset/fast-onions-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"arktype": patch
---

Allow instanceof abstract classes, avoid treating some function props as morphs
12 changes: 12 additions & 0 deletions dev/test/instanceof.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ describe("instanceof", () => {
"Must be an instance of TypeError (was Error)"
)
})
it("abstract", () => {
abstract class Base {
abstract foo: string
}
class Sub extends Base {
foo = ""
}
const t = type(["instanceof", Base])
attest(t.infer).typed as Base
const sub = new Sub()
attest(t(sub).data).equals(sub)
})
it("builtins not evaluated", () => {
const t = type(["instanceof", Date])
attest(t.infer).types.toString("Date")
Expand Down
53 changes: 31 additions & 22 deletions dev/test/morph.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { describe, it } from "mocha"
import { ark, intersection, morph, scope, type, union } from "../../src/main.js"
import type { Problem, Type } from "../../src/main.js"
import { ark, intersection, morph, scope, type, union } from "../../src/main.js"
import type { Out } from "../../src/parse/ast/morph.js"
import { writeUndiscriminatableMorphUnionMessage } from "../../src/parse/ast/union.js"
import { attest } from "../attest/main.js"

describe("morph", () => {
it("base", () => {
const t = type(["boolean", "|>", (data) => `${data}`])
attest(t).typed as Type<(In: boolean) => string>
attest(t).typed as Type<(In: boolean) => Out<string>>
attest(t.infer).typed as string
attest(t.node).snap({ boolean: { rules: {}, morph: "(function)" } })
const result = t(true)
Expand All @@ -19,7 +20,7 @@ describe("morph", () => {
})
it("endomorph", () => {
const t = type(["boolean", "|>", (data) => !data])
attest(t).typed as Type<(In: boolean) => boolean>
attest(t).typed as Type<(In: boolean) => Out<boolean>>
const result = t(true)
if (result.problems) {
return result.problems.throw()
Expand All @@ -28,7 +29,7 @@ describe("morph", () => {
})
it("from type", () => {
const t = type(["string>5", "|>", ark.parsedDate])
attest(t).typed as Type<(In: string) => Date>
attest(t).typed as Type<(In: string) => Out<Date>>
attest(t("5/21/1993").data?.getDate()).equals(21)
attest(t("foobar").problems?.summary).snap(
"Must be a valid date (was 'foobar')"
Expand All @@ -40,7 +41,7 @@ describe("morph", () => {
"|>",
(n, problems) => (n === 0 ? problems.mustBe("non-zero") : 100 / n)
])
attest(divide100By).typed as Type<(In: number) => number>
attest(divide100By).typed as Type<(In: number) => Out<number>>
attest(divide100By(5).data).equals(20)
attest(divide100By(0).problems?.summary).snap(
"Must be non-zero (was 0)"
Expand All @@ -67,7 +68,7 @@ describe("morph", () => {
it("at path", () => {
const t = type({ a: ["string", "|>", (data) => data.length] })
attest(t).typed as Type<{
a: (In: string) => number
a: (In: string) => Out<number>
}>
const result = t({ a: "four" })
if (result.problems) {
Expand All @@ -82,7 +83,9 @@ describe("morph", () => {
lengthOfString: ["string", "|>", (data) => data.length],
mapToLengths: "lengthOfString[]"
}).compile()
attest(types.mapToLengths).typed as Type<((In: string) => number)[]>
attest(types.mapToLengths).typed as Type<
((In: string) => Out<number>)[]
>
const result = types.mapToLengths(["1", "22", "333"])
if (result.problems) {
return result.problems.throw()
Expand All @@ -91,7 +94,7 @@ describe("morph", () => {
})
it("object inference", () => {
const t = type([{ a: "string" }, "|>", (data) => `${data}`])
attest(t).typed as Type<(In: { a: string }) => string>
attest(t).typed as Type<(In: { a: string }) => Out<string>>
})
it("intersection", () => {
const $ = scope({
Expand All @@ -101,7 +104,7 @@ describe("morph", () => {
bAndA: () => $.type("b&a")
})
const types = $.compile()
attest(types.aAndB).typed as Type<(In: 3.14) => string>
attest(types.aAndB).typed as Type<(In: 3.14) => Out<string>>
attest(types.aAndB.node).snap({
number: { rules: { value: 3.14 }, morph: "(function)" }
})
Expand All @@ -115,7 +118,7 @@ describe("morph", () => {
c: "a&b"
})
const types = $.compile()
attest(types.c).typed as Type<(In: { a: 1; b: 2 }) => string>
attest(types.c).typed as Type<(In: { a: 1; b: 2 }) => Out<string>>
attest(types.c.node).snap({
object: {
rules: {
Expand All @@ -135,7 +138,9 @@ describe("morph", () => {
aOrB: "a|b",
bOrA: "b|a"
}).compile()
attest(types.aOrB).typed as Type<boolean | ((In: number) => string)>
attest(types.aOrB).typed as Type<
boolean | ((In: number) => Out<string>)
>
attest(types.aOrB.node).snap({
number: { rules: {}, morph: "(function)" },
boolean: true
Expand All @@ -150,7 +155,7 @@ describe("morph", () => {
c: "a&b"
}).compile()
attest(types.c).typed as Type<{
a: (In: 1) => number
a: (In: 1) => Out<number>
}>
attest(types.c.node).snap({
object: {
Expand All @@ -168,7 +173,7 @@ describe("morph", () => {
}).compile()
attest(types.c).typed as Type<
| {
a: (In: number) => string
a: (In: number) => Out<string>
}
| {
a: (...args: any[]) => unknown
Expand Down Expand Up @@ -200,7 +205,7 @@ describe("morph", () => {
b: () => $.morph("a", (n) => n === 0)
})
const types = $.compile()
attest(types.b).typed as Type<(In: string) => boolean>
attest(types.b).typed as Type<(In: string) => Out<boolean>>
attest(types.b.node).snap({
string: { rules: {}, morph: ["(function)", "(function)"] }
})
Expand All @@ -211,7 +216,7 @@ describe("morph", () => {
b: () => $.morph({ a: "a" }, ({ a }) => a === 0)
})
const types = $.compile()
attest(types.b).typed as Type<(In: { a: string }) => boolean>
attest(types.b).typed as Type<(In: { a: string }) => Out<boolean>>
attest(types.b.node).snap({
object: { rules: { props: { a: "a" } }, morph: "(function)" }
})
Expand All @@ -224,7 +229,7 @@ describe("morph", () => {
"|>",
({ a }) => a === 0
])
attest(t).typed as Type<(In: { a: string }) => boolean>
attest(t).typed as Type<(In: { a: string }) => Out<boolean>>
attest(t.node).snap({
object: {
rules: {
Expand All @@ -241,7 +246,9 @@ describe("morph", () => {
c: () => $.type("a|b")
})
const types = $.compile()
attest(types.c).typed as Type<[boolean] | ((In: [string]) => string[])>
attest(types.c).typed as Type<
[boolean] | ((In: [string]) => Out<string[]>)
>
attest(types.c.node).snap({
object: [
{
Expand Down Expand Up @@ -336,7 +343,7 @@ describe("morph", () => {
const t = $.type("a|b")
attest(t).typed as Type<
| {
a: (In: string) => string
a: (In: string) => Out<string>
}
| {
a: boolean
Expand Down Expand Up @@ -391,15 +398,17 @@ describe("morph", () => {
return result
}
])
attest(parsedInt).typed as Type<(In: string) => number>
attest(parsedInt).typed as Type<(In: string) => Out<number>>
attest(parsedInt("5").data).snap(5)
attest(parsedInt("five").problems?.summary).snap(
"Must be an integer string (was 'five')"
)
})
it("nullable return", () => {
const toNullableNumber = type(["string", "|>", (s) => s.length || null])
attest(toNullableNumber).typed as Type<(In: string) => number | null>
attest(toNullableNumber).typed as Type<
(In: string) => Out<number | null>
>
})
it("undefinable return", () => {
const toUndefinableNumber = type([
Expand All @@ -408,7 +417,7 @@ describe("morph", () => {
(s) => s.length || undefined
])
attest(toUndefinableNumber).typed as Type<
(In: string) => number | undefined
(In: string) => Out<number | undefined>
>
})
it("null or undefined return", () => {
Expand All @@ -419,7 +428,7 @@ describe("morph", () => {
s.length === 0 ? undefined : s.length === 1 ? null : s.length
])
attest(toMaybeNumber).typed as Type<
(In: string) => number | null | undefined
(In: string) => Out<number | null | undefined>
>
})
})
6 changes: 3 additions & 3 deletions src/parse/ast/intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { disjointDescriptionWriters } from "../../nodes/compose.js"
import type { asConst, evaluate, isAny, List } from "../../utils/generics.js"
import { objectKeysOf } from "../../utils/generics.js"
import type { Path, pathToString } from "../../utils/paths.js"
import type { ParsedMorph } from "./morph.js"
import type { Out, ParsedMorph } from "./morph.js"

export type inferIntersection<l, r> = [l] extends [never]
? never
Expand All @@ -16,9 +16,9 @@ export type inferIntersection<l, r> = [l] extends [never]
: l extends ParsedMorph<infer lIn, infer lOut>
? r extends ParsedMorph
? never
: (In: evaluate<lIn & r>) => lOut
: (In: evaluate<lIn & r>) => Out<lOut>
: r extends ParsedMorph<infer rIn, infer rOut>
? (In: evaluate<rIn & l>) => rOut
? (In: evaluate<rIn & l>) => Out<rOut>
: intersectObjects<l, r> extends infer result
? result
: never
Expand Down
8 changes: 6 additions & 2 deletions src/parse/ast/morph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ export type validateMorphTuple<def extends TupleExpression, $> = readonly [

export type Morph<i = any, o = unknown> = (In: i, problems: Problems) => o

export type ParsedMorph<i = any, o = unknown> = (In: i) => o
export type Out<o = unknown> = readonly ["|>", o]

export type ParsedMorph<i = any, o = unknown> = (In: i) => Out<o>

export type inferMorph<inDef, morph, $> = morph extends Morph
? (In: asIn<inferDefinition<inDef, $>>) => inferMorphOut<ReturnType<morph>>
? (
In: asIn<inferDefinition<inDef, $>>
) => Out<inferMorphOut<ReturnType<morph>>>
: never

type inferMorphOut<out> = [out] extends [CheckResult<infer t>]
Expand Down
9 changes: 5 additions & 4 deletions src/scopes/ark.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Out } from "../parse/ast/morph.js"
import { jsObjects, jsObjectsScope } from "./jsObjects.js"
import type { Space } from "./scope.js"
import { rootScope, scope } from "./scope.js"
Expand Down Expand Up @@ -60,10 +61,10 @@ export type PrecompiledDefaults = {
email: string
uuid: string
semver: string
json: (In: string) => unknown
parsedNumber: (In: string) => number
parsedInteger: (In: string) => number
parsedDate: (In: string) => Date
json: (In: string) => Out<unknown>
parsedNumber: (In: string) => Out<number>
parsedInteger: (In: string) => Out<number>
parsedDate: (In: string) => Out<Date>
// jsObjects
Function: (...args: any[]) => unknown
Date: Date
Expand Down
4 changes: 3 additions & 1 deletion src/utils/generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ export const isKeyOf = <k extends string | number, obj extends object>(
obj: obj
): k is Extract<keyof obj, k> => k in obj

export type constructor<instance = unknown> = new (...args: any[]) => instance
export type constructor<instance = unknown> = abstract new (
...args: any[]
) => instance

export type instanceOf<classType extends constructor<any>> =
classType extends constructor<infer Instance> ? Instance : never
Expand Down