arktypeio / arktype

TypeScript's 1:1 validator, optimized from editor to runtime
https://arktype.io
MIT License
3.65k stars 48 forks source link

TypeScript declaration error: a type annotation is necessary #1031

Open jacob-alford opened 3 weeks ago

jacob-alford commented 3 weeks ago

Report a bug

Hello, I have an issue while exporting arktype schemas using TypeScript. I get the following error:

The inferred type of 'Foo' cannot be named without a reference to '.pnpm/@arktype+schema@0.1.18/node_modules/@arktype/schema'. This is likely not portable. A type annotation is necessary.ts(2742)

🔎 Search Terms

cannot be named, without reference to, likely not portable, type annotation necessary, declaration files

🧩 Context

🧑‍💻 Repro

// Paste reproduction code here
import * as ark from 'arktype'

export const Foo = ark.type('parse.integer')

The above will result in the error found at the top of this issue. Note: TS must be configured to emit declaration files in order to see the error

ssalbdivad commented 2 weeks ago

This is a very common error in TS I have encountered many times but the circumstances are quite hard to pinpoint. See https://github.com/microsoft/TypeScript/issues/42873

Does adding @arktype/schema to your dependencies directly resolve the issue? If so, I can eventually look into a method for publishing such that the types will always be resolved, maybe by bundling it.

NamesMT commented 2 weeks ago

+1, in my case I was declaring an optional string type and the Type['inferIn'] of it is string.moreThanLength instead of just string like the non optional key.

image

I'm using @hono/arktype-validator

jacob-alford commented 1 week ago

@ssalbdivad Hello, yes adding @arktype/schema to dependencies did solve that issue. However, I ran into a similar issue now after resolving. It doesn't apply to my original example exactly, but here is a reproduction for my current issue:

Error:

Exported variable 'Foo' has or is using name 'constrained' from external module "/Users/jalford/Documents/git/project/node_modules/.pnpm/@arktype+schema@0.1.22/node_modules/@arktype/schema/out/ast" but cannot be named.

Reproduction:

import * as ark from 'arktype'

export const Foo = ark.type(/\d+_\d+/).pipe((s) => {
  const split = s.split('_')
  const fst = split[0]
  const snd = split[1]
  if (fst === undefined || snd === undefined) {
    return `Received unexpectedly invalid string ${split}, expected a string matching the pattern /\\d+_\\d/`
  }
  return Number(`${fst}${snd}`)
}, ark.type('integer'))

I believe the issue comes from .pipe(..., ark.type("integer")).

Just for reference, I'm taking a string like this: 123_45 to a number: 12345.

ssalbdivad commented 1 week ago

Hmm, well that is unfortunate and also very environment-dependent.

I would consider bundling @arktype/schema as mentioned. I've opened an issue for that here:

https://github.com/arktypeio/arktype/issues/1049

As a potential stopgap, I will also try re-exporting inferred symbols from arktype (including constrained) in the next release to see if that helps.

jacob-alford commented 1 week ago

Thank you for your help!

jacob-alford commented 1 week ago

FWIW we switched from pnpm to yarn 4 and the issue is still happening.

However, the initial issue that I opened with (i.e. The inferred type of 'Foo'...) does not happen with our new yarn 4 config. So maybe it's a bug with pnpm workspaces, but I don't think it's the fault of arktype. That should affect your #1049