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

Unable to assign string-embedded default value to type including scoped keyword #1018

Closed
ssalbdivad opened this issue Jun 18, 2024 · 0 comments

Comments

@ssalbdivad
Copy link
Member

Report a bug

🔎 Search Terms

default value scope alias not assignable literal

🧩 Context

  • ArkType version: 2.0.0-beta.0
  • TypeScript version (5.1+): 5.4.5
  • Other context you think may be relevant (JS flavor, OS, etc.):

🧑‍💻 Repro

This test currently fails because we are not able to infer specialNumber in this context:

it("validated default in scope", () => {
  const $ = scope({
    specialNumber: "number",
    // "5 is not assignable to specialNumber "
    obj: { foo: "string", bar: "specialNumber = 5" },
  });
});

This is due to this workaround in ark/type/parser/objectLiteral.ts in validateDefaultValueString:

type validateDefaultValueString =
// other context
[
  // check against the output of the type since morphs will not occur
  // we currently can't parse string embedded defaults for non-global keywords
  distillOut<inferString<baseDef, {}, args>>,
  // a default value should never have In/Out, so which side we choose is irrelevant
  // we will never need a scope here as we're just trying to infer a UnitLiteral
  distillOut<inferString<defaultDef, {}, args>>
] extends [infer base, infer defaultValue]

The reason we need to pass {} in the top def instead of $ (which fixes the issue) is that passing $ and then comparing the values breaks other inference, namely thunks and piping (you should see errors across the repo if you change this back to $).

For now the workaround is to just use a tuple default, e.g:

it("validated default in scope", () => {
  const $ = scope({
    specialNumber: "number",
    obj: { foo: "string", bar: ["specialNumber", "=", 5] },
  });
});

This may not be resolvable without changes to TS, but could be fixed by #1017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done (merged or closed)
Development

No branches or pull requests

1 participant