Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

TypeScript declaration error: a type annotation is necessary #1031

Closed
jacob-alford opened this issue Jul 1, 2024 · 9 comments
Closed

TypeScript declaration error: a type annotation is necessary #1031

jacob-alford opened this issue Jul 1, 2024 · 9 comments
Assignees
Labels
bug Something isn't working

Comments

@jacob-alford
Copy link

jacob-alford commented Jul 1, 2024

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

  • ArkType version: 2.0.0-dev.26
  • TypeScript version (5.1+): 5.5.2
  • Other context you think may be relevant (JS flavor, OS, etc.): pnpm, turbo monorepo, using declaration: true in tsconfig

🧑‍💻 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

@jacob-alford jacob-alford added the bug Something isn't working label Jul 1, 2024
@github-project-automation github-project-automation bot moved this to To do in arktypeio Jul 1, 2024
@ssalbdivad
Copy link
Member

This is a very common error in TS I have encountered many times but the circumstances are quite hard to pinpoint. See microsoft/TypeScript#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.

@ssalbdivad ssalbdivad moved this from To do to Backlog in arktypeio Jul 9, 2024
@NamesMT
Copy link

NamesMT commented Jul 9, 2024

+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
Copy link
Author

@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
Copy link
Member

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:

#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
Copy link
Author

Thank you for your help!

@jacob-alford
Copy link
Author

jacob-alford commented Jul 15, 2024

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

@ssalbdivad
Copy link
Member

Closing this for now, but will continue to track additional remediation for this as part of #1049

@github-project-automation github-project-automation bot moved this from Backlog to Done (merged or closed) in arktypeio Jul 24, 2024
@NamesMT
Copy link

NamesMT commented Jul 26, 2024

Another possible solution for this issue is adding @ark/schema into tsconfig.json's compilerOptions.types array:

{
  "compilerOptions": {
    // ...
    "types": [
      "@ark/schema"
    ]
  }
}

@dikatok
Copy link

dikatok commented Aug 8, 2024

Another possible solution for this issue is adding @ark/schema into tsconfig.json's compilerOptions.types array:

{
  "compilerOptions": {
    // ...
    "types": [
      "@ark/schema"
    ]
  }
}

confirmed working on my workspace, without needing to install the schema or util dependencies

EDIT The dependencies resolution is broken again on 2.0.0-beta.5 😞

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
Status: Done (merged or closed)
Development

No branches or pull requests

4 participants