Skip to content

TypeScript declaration error: a type annotation is necessary #1031

Closed

Description

@rae-hill

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/@[email protected]/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

Activity

  1. added
    bug
    on Jul 1, 2024
  2. ssalbdivad commented on Jul 9, 2024

    @ssalbdivad
    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.

  3. moved this from To do to Backlog in arktypeioon Jul 9, 2024
  4. NamesMT commented on Jul 9, 2024

    @NamesMT

    +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

  5. rae-hill commented on Jul 10, 2024

    @rae-hill
    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/@[email protected]/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.

  6. ssalbdivad commented on Jul 10, 2024

    @ssalbdivad
    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.

  7. rae-hill commented on Jul 10, 2024

    @rae-hill
    Author

    Thank you for your help!

  8. rae-hill commented on Jul 15, 2024

    @rae-hill
    Author

    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

  9. ssalbdivad commented on Jul 24, 2024

    @ssalbdivad
    Member

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

  10. moved this from Backlog to Done (merged or closed) in arktypeioon Jul 24, 2024
  11. NamesMT commented on Jul 26, 2024

    @NamesMT

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

    {
      "compilerOptions": {
        // ...
        "types": [
          "@ark/schema"
        ]
      }
    }
  12. dikatok commented on Aug 8, 2024

    @dikatok

    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 😞

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