arktypea
arktypeโ€ข9mo ago
Flosi21

type inference resolves types from scopes to never in union

When using types from scopes in a union, arktype will resolve some of them to be "never", however if you insert their definition directly it resolves the type correctly

Here is an example. If you paste this in the playground and hover of the out, the type will resolve to "never" in some places.

Interestingly the .expression tab in the playground resolves the native Typescript expression correctly

Is this a bug or am I misusing scopes in some way?

import { type } from "arktype";

const formScope = type.scope({
  "form.number": "string.numeric.parse",
  "form.integer": "string.integer.parse",
  "form.string": "string > 0",
  "form.checkbox": type("true|false|undefined", "=>", (c) => (c === undefined ? false : c)),
  "form.date": /^\d{2}\.\d{2}\.\d{4}$/,
  "form.year": type(/^\d{4}$/, "=>", (c) => Number.parseInt(c, 10)),
  "form.yearmonth": /^\d{2}\/\d{4}$/ ,
  "form.yesno.yes": "'yes'",
  "form.yesno.no": "'no'",
  "form.yesno": ["form.yesno.yes | form.yesno.no", "=>", (c) => c === "yes"],
 });

const arkScope = type.scope({
  ...formScope.export(),
});

const ark = arkScope;

const Thing = ark.type
  .or(
    ark.type
      .or(
        {
          hasWorkPermit: "form.yesno.yes",
          workPermit: {
            validUntil: "form.yearmonth",
          },
        },
        {
          hasWorkPermit: "form.yesno.no",
        },
      )
      .and({
        hasTemporaryResidence: "form.yesno.yes",
        temporaryResidence: {
          validUntil: "form.yearmonth",
        },
      }),
    {
      hasTemporaryResidence: "form.yesno.no",
    },
  )
  .and({
    inGermanySince: "form.yearmonth",
  });

const out = Thing({})
Was this page helpful?