Skip to content

Validated completions available for string props are not available for shallow strings #53997

Closed
@ssalbdivad

Description

@ssalbdivad

Bug Report

I encountered this bug while adding autocomplete support for ArkType. The goal is for completions to behave the same way whether they are parsed at an object key like type({a: "string|"}) or via a shallow string definition like type("string|").

However, autocompletions for expressions like these unexpectedly fail for the shallow case despite succeeding when defined in an object.

🔎 Search Terms

completions, autocomplete, strings, expressions, parsing, arktype

🕗 Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

💻 Code

type keyword = "foo" | "bar" | "baz"

type validateString<s> = s extends keyword
    ? s
    : s extends `${infer left extends keyword}|${infer right}`
    ? right extends keyword
        ? s
        : `${left}|${keyword}`
    : keyword

type isUnknown<t> = unknown extends t
    ? [t] extends [{}]
        ? false
        : true
    : false

type validate<def> = def extends string
    ? validateString<def>
    : isUnknown<def> extends true
    ? keyword
    : {
          [k in keyof def]: validate<def[k]>
      }

const parse = <def>(def: validate<def>) => def

// typing "f" autocompletes to "foo"
const shallowKeyword = parse("foo")

// typing "foo|b" does not autocomplete but yields the following type error if left incomplete:
// Argument of type '"foo|b"' is not assignable to parameter of type '"foo|foo" | "foo|bar" | "foo|baz"'
// @ts-expect-error
const shallowExpression = parse("foo|b")

// typing "f" autocompletes to "foo"
const deepKeyword = parse({ k: "foo" })

// typing "foo|b" autocompletes to "foo|bar" | "foo|baz"
const deepExpression = parse({ k: "foo|bar" })

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions