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 import in TS project with "moduleResolution": "NodeNext", #198

Open
2 tasks done
cyberwombat opened this issue Oct 15, 2022 · 12 comments · May be fixed by #199
Open
2 tasks done

Unable to import in TS project with "moduleResolution": "NodeNext", #198

cyberwombat opened this issue Oct 15, 2022 · 12 comments · May be fixed by #199

Comments

@cyberwombat
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

NA

Plugin version

4.0.0

Node.js version

16.17

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.4

Description

I am updating my TS code to use ESM which requires having "moduleResolution": "NodeNext" or "moduleResolution": "Node16". In doing so my code (whether I use import S... or import * as S.... now gives me errors when I do:

S.object() or S.string() etc with:

Property 'object' does not exist on type 'typeof import("/.../node_modules/fluent-json-schema/types/FluentJSONSchema")'.ts(2339)

I can provide a sample repo but I think if you just add `"moduleResolution": "NodeNext"`` to any TS sample it should do it.

Steps to Reproduce

Use `"moduleResolution": "NodeNext"`` in a TS project

Expected Behavior

No response

cesarvspr added a commit to cesarvspr/fluent-json-schema that referenced this issue Oct 27, 2022
@cesarvspr
Copy link

cesarvspr commented Oct 27, 2022

Will be potentially fixed in #199

@aboutlo
Copy link
Collaborator

aboutlo commented Oct 28, 2022

@cyberwombat can you test it with #199 fixes?

@cyberwombat
Copy link
Author

cyberwombat commented Oct 28, 2022

I think still an issue, additionally it looks like ExtendedSchema is not exported anymore. Heres a screenshot of errors in VSCode with 199.

Screen Shot 2022-10-28 at 8 02 32 AM

What I currently have to do to make it work:

import FJS from 'fluent-json-schema'
export const S = FJS.default 
export const fooSchema =  S.string().maxLength(200)

Here's my tsconfig:

{
  "extends": "@tsconfig/node16/tsconfig.json",
  "compilerOptions": {
    "useUnknownInCatchVariables": false,
    "target": "es2020",
    "lib": ["es2020"],
    "module": "node16",
    "moduleResolution": "NodeNext",
    "esModuleInterop": true, // Eases ESM support
    "types": ["node"],
    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "strict": true,
    "strictNullChecks": true,
    "alwaysStrict": true,
    //importsNotUsedAsValues
    // "noImplicitAny": false,
    "incremental": true,
    "noImplicitReturns": false,
    "noImplicitThis": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noEmit": true,
    "noFallthroughCasesInSwitch": false,
    "inlineSourceMap": true,
    "inlineSources": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    // "typeRoots": ["./node_modules/@types"]
    "resolveJsonModule": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
    },
    "typeRoots": ["node_modules/@types", "typings"],
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

TS: 4.8.4

@cesarvspr
Copy link

I think still an issue, additionally it looks like ExtendedSchema is not exported anymore. Heres a screenshot of errors in VSCode with 199.

Fixed in the last commit, please check if you find other issues

@cyberwombat
Copy link
Author

cyberwombat commented Oct 28, 2022

That fixed the ExtendedSchema but I am still having original issue with needed to use .default

import S from 'fluent-json-schema'

export const commonLabelSchema = S.default.string()

@cesarvspr
Copy link

cesarvspr commented Oct 28, 2022

@cyberwombat can I see what you are extending @tsconfig/node16/tsconfig.json ?

I was able to use

import S, { FluentSchemaError, ExtendedSchema } from "..";

console.log("isFluentSchema:", S.object().isFluentJSONSchema);
const schema = S.string()

If we find out what is happening would be nice to reproduce inside the FluentJSONSchema.test-d.ts

@cyberwombat
Copy link
Author

@cesarvspr @tsconfig/node16/tsconfig.json extend is not necessary. I can remove it with same issue. What is your tsconfig?

@cesarvspr
Copy link

@cyberwombat

I was able to use you provided tsconfig.json file and my own which is

{
  "compilerOptions": {
    "useUnknownInCatchVariables": false,
    "target": "es2020",
    "lib": ["es2020"],
    "module": "node16",
    "moduleResolution": "NodeNext",
  }
}

here you can see my test:
minimal_project.zip

@cyberwombat
Copy link
Author

You need "type":"module" in package.json as well for ESM. It fails then.

@cesarvspr
Copy link

you are right @cyberwombat, but I could not find a solution for this specific case yet.

Btw, why are you using the "type":"module"?

@cyberwombat
Copy link
Author

I am migrating to 100% esm and that's part of the jump. If I don't then my code stays CJS when imported. I don't know of a solution except to build as both esm and cjs and using the exporrts field in package.json od doing the full esm jump as many packages are doing. I heard there is a wrapper out there that can be used to help male the transition but I think it still requires a build output though I am not sure. Maybe this helps https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

@cesarvspr
Copy link

@climba03003 Would you please help with this? I have a PR ready that just needs to work when we have "type":"module" on the package.json

@Uzlopak Uzlopak linked a pull request Nov 21, 2022 that will close this issue
4 tasks
@cesarvspr cesarvspr linked a pull request Nov 21, 2022 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants