Repository navigation
Closed
Description
Report a bug
I've found another unexpected behaviour with some complex type setups. The issue only occurs when null is part of the union, and when it is a recursive one.
🔎 Search Terms
- recursive nullable union
🧩 Context
System:
OS: macOS 14.2.1
CPU: (10) arm64 Apple M2 Pro
Memory: 737.78 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.18.1 - ~/.nvm/versions/node/v20.18.1/bin/node
npm: 10.8.2 - ~/.nvm/versions/node/v20.18.1/bin/npm
npmPackages:
@ark/attest: ^0.30.0 => 0.30.0
@ark/fs: 0.26.0
@ark/schema: 0.39.0
@ark/util: 0.26.0
arktype: ^2.0.4 => 2.0.4
typescript: ^5.7.3 => 5.7.3
🧑💻 Repro
import { scope } from "arktype";
export const ruleset = scope({
TypeX: {
id: "string",
"+": "reject",
},
// works
TypeA: {
label: "string",
id: "string",
"result?": "TypeA | TypeX",
"+": "reject",
},
// does not work
TypeB: {
label: "string",
id: "string",
"result?": "TypeB | TypeX | null",
"+": "reject",
},
// works
TypeC: {
label: "string",
id: "string",
"result?": "TypeA | TypeX | null",
"+": "reject",
},
});
const types = ruleset.export();
const data = {
label: "hi",
id: "C",
result: {label: "A", id: "B"},
}
console.log(types.TypeA(data).summary); // undefined
console.log(types.TypeB(data).summary); // result.label must be removed
console.log(types.TypeC(data).summary); // undefinedTypeA and TypeC handles the input data as I expected. TypeB rejects it - the only difference is that result can now be null.
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsDone (merged or closed)
Activity
ssalbdivad commentedon Jan 29, 2025
Looks like this is another JIT-related cyclic compilation bug (see #1188), as you can workaround it by setting
jitless: truein your scope config:trsaunders commentedon Jan 30, 2025
Thanks for the tip about
jitlessthat did indeed allow the example in the report to work, but did not help in my more complex real case. After a bit of experimentation I found thatjitlessdoes not have any effect if the offending type is referenced by aRecord:If you comment out
TypeD: ...then the result is as expected. The presence ofTypeEandTypeFdo not seem to cause a problem.ssalbdivad commentedon Jan 30, 2025
@trsaunders For now then I would recommend invoking these recursive references through a
narrowinstead of allowing the type system to compute them directly.trsaunders commentedon Feb 10, 2025
@ssalbdivad thanks for the suggestion about using
narrow, I'm only just getting back to this now. Is this the sort of thing you were suggesting (it appears to work), or am I missing a better way? Is there a way to pass throughresfrom the narrow?fix(schema): dispatch interpreted unions on their discriminant
test(type): restore 2.2.7's cyclic realWorld tests