fluent-json-schema icon indicating copy to clipboard operation
fluent-json-schema copied to clipboard

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

Open cyberwombat opened this issue 3 years ago • 12 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] 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

cyberwombat avatar Oct 15 '22 19:10 cyberwombat

Will be potentially fixed in https://github.com/fastify/fluent-json-schema/pull/199

cesarvspr avatar Oct 27 '22 04:10 cesarvspr

@cyberwombat can you test it with #199 fixes?

aboutlo avatar Oct 28 '22 11:10 aboutlo

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

cyberwombat avatar Oct 28 '22 15:10 cyberwombat

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

cesarvspr avatar Oct 28 '22 15:10 cesarvspr

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()

cyberwombat avatar Oct 28 '22 15:10 cyberwombat

@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

cesarvspr avatar Oct 28 '22 16:10 cesarvspr

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

cyberwombat avatar Oct 28 '22 16:10 cyberwombat

@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

cesarvspr avatar Oct 28 '22 16:10 cesarvspr

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

cyberwombat avatar Oct 29 '22 04:10 cyberwombat

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

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

cesarvspr avatar Oct 30 '22 05:10 cesarvspr

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

cyberwombat avatar Oct 30 '22 13:10 cyberwombat

@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

cesarvspr avatar Nov 20 '22 18:11 cesarvspr