Unable to import in TS project with "moduleResolution": "NodeNext",
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
Will be potentially fixed in https://github.com/fastify/fluent-json-schema/pull/199
@cyberwombat can you test it with #199 fixes?
I think still an issue, additionally it looks like ExtendedSchema is not exported anymore. Heres a screenshot of errors in VSCode with 199.
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
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
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 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 @tsconfig/node16/tsconfig.json extend is not necessary. I can remove it with same issue. What is your tsconfig?
@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
You need "type":"module" in package.json as well for ESM. It fails then.
you are right @cyberwombat, but I could not find a solution for this specific case yet.
Btw, why are you using the "type":"module"?
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
@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