Skip to content

type inference resolves custom types from scopes to be "never" when using them in a union typeΒ #1422

Closed

Description

@simonwkla

Report a bug

πŸ”Ž Search Terms

  • never
  • union
  • discriminated union
  • scope

🧩 Context

  • ArkType version: 2.1.19
  • TypeScript version (5.1+): 5.7.3
  • Other context you think may be relevant (JS flavor, OS, etc.):

πŸ§‘β€πŸ’» Repro

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 of the fields will resolve to "never"

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?

PlaygroundLink

import { type } from "arktype"

const formScope = type.scope({
	"form.yesno.yes": "'yes'",
	"form.yesno.no": "'no'"
})

const Thing = formScope.type.or(
	{
		hasTemporaryResidence: "form.yesno.yes"
	},
	{
		hasTemporaryResidence: "form.yesno.no"
	}
)

const out = Thing({})

Activity

  1. added
    bug
    on Apr 15, 2025
  2. simonwkla commented on Apr 15, 2025

    @simonwkla
    ContributorAuthor

    I did some digging and the problem seems to be that in the NaryUnionParser the scope is not passed to type.infer

    When i change these lines

    arktype/ark/type/nary.ts

    Lines 22 to 25 in 2614ce8

    <const a, const b, r = Type<type.infer<a> | type.infer<b>, $>>(
    a: type.validate<a, $>,
    b: type.validate<b, $>
    ): r extends infer _ ? _ : never

    to

        <const a, const b, r = Type<type.infer<a, $> | type.infer<b, $>, $>>(
            a: type.validate<a, $>,
            b: type.validate<b, $>
        ): r extends infer _ ? _ : never

    it works.

    Is there are reason why the scope is not passed to type.infer here?

  3. added a commit that references this issue on Apr 15, 2025
  4. moved this from To do to In progress in arktypeioon Apr 15, 2025
  5. added 6 commits that reference this issue on Apr 15, 2025
  6. moved this from In progress to Done (merged or closed) in arktypeioon Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

  • Status
    Done (merged or closed)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions