fix(typescript): Fix incorrect type generated for glob route parameters | Add type tests#6364
fix(typescript): Fix incorrect type generated for glob route parameters | Add type tests#6364Tobbe merged 26 commits intoredwoodjs:mainfrom
Conversation
* 'main' of github.com:redwoodjs/redwood: fix(deps): update dependency @graphql-yoga/common to v2.12.12 (redwoodjs#6349) fix(test-project): revert @redwoodjs/core to rc Update yarn.lock v2.2.4 bugfix replace slash in tailwind config on windows (redwoodjs#6203) bugfix replace slash in tailwind config on windows (redwoodjs#6203) chore(deps): update dependency @testing-library/dom to v8.17.1 (redwoodjs#6351) Update yarn.lock Use try/catch to access unauthenticated (redwoodjs#6358) issue#5852 added windows fix for nodeFileTrace (redwoodjs#6325) Handle special props `ref` and `key` in path and search params (redwoodjs#5537) Use try/catch to access unauthenticated (redwoodjs#6358) feat(codemod): Add codemod to make relation resolvers partial (redwoodjs#6342)
|
Questions:
|
Switch to expectAssignable
|
@Tobbe - unbelievably I made the glob thing work too - but I broke down the types into parts that will be atleast a little bit more grokable. Please do suggest better names - I notice I'm mixing up the "verb form" e.g. Please let me know what you think when you have a chance to look - maybe I'll add the tests for the PrismaMerge thing in another pr! |
because *.d.ts don't get copied to dist
| @@ -0,0 +1,3 @@ | |||
| { | |||
| "extends": "../../tsconfig.json" | |||
There was a problem hiding this comment.
Try adding the following lines and importing from .ts file. Does it help?
"compilerOptions": {
"composite": false,
}There was a problem hiding this comment.
Thanks for looking into it!
Yep I had to set composite, but also set incremental - because the base tsconfig contains tsBuildInfo - which needs one of these flags. I'm not 100% sure the impact this has though, but since its only for the tsd test environment should be ok, I think
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": false,
"incremental": true
},
}
There was a problem hiding this comment.
Actually had to add all of these 😢 - because of conflicting flags set in the original config. I suppose these settings only affect the build - which we're not in the case of type tests
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": false,
"incremental": true,
"declaration": false,
"declarationMap": false,
"emitDeclarationOnly": false
},
}
Is the fact that the tsconfig in the tests drifting away from the project config an issue?
You're right about composite definitely being the problem... but if I check the config with tsc here:
❯ yarn tsc -p src/__typetests__ --showConfig
{
"compilerOptions": {
"target": "esnext",
"moduleResolution": "node",
"module": "esnext",
"declarationMap": true,
"emitDeclarationOnly": true,
"sourceMap": true,
"composite": true,
"strict": true,
"esModuleInterop": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"jsx": "preserve",
"skipLibCheck": true,
"resolveJsonModule": true,
"tsBuildInfoFile": "../../dist/tsconfig.tsbuildinfo",
"baseUrl": "../..",
"rootDir": "..",
"outDir": "../../dist",
"paths": {
"src/*": [
"./src/*"
]
}
},
"files": [
"../ActivePageContext.tsx",
"../PageLoadingContext.tsx",
"../Set.tsx",
"../a11yUtils.ts",
"../active-route-loader.tsx",
"../history.tsx",
"../index.ts",
"../links.tsx",
"../location.tsx",
"../params.tsx",
"../route-announcement.tsx",
"../route-focus.tsx",
"../routeParamsTypes.ts", 👈 definitely here
"../router-context.tsx",
"../router.tsx",
"../splash-page.tsx",
"../useIsMounted.ts",
"../util.ts",
"../../ambient.d.ts"
],
"include": [
"../../src",
"../.././ambient.d.ts"
],
"exclude": [
"../../../../dist",
"../../../../node_modules",
"../../../../**/__tests__",
"../../../../**/__mocks__",
"../../../../**/*.test.*"
]
}
There was a problem hiding this comment.
Yes, the file is included. I see the same config from inside tsd-lite. It is using TS compiler’s APIs to find and read config.
The error we saw comes from TS compiler and it looks misleading. Here is an experiment:
- Add empty
tsconfig.jsonto__typetests__folder. - Run
yarn tsc -p src/__typetests__. All works. - Add
"compilerOptions": {"composite": true}to the sametsconfig.jsonand run the same command. - Oh.. that’s the error we saw.. And it came from
tscthis time. - Now also add
"references": [{ "path": "../../" }]to the sametsconfig.jsonand run the command.
All works again. Isn’t it that with "composite": true each directory with tsconfig.json turns into a project? So in this case the compiler complains, because it sees src and __typetests__ as separate projects.
By the way, tsd-lite wraps TS compilers’s APIs to typecheck and compare types. If tsc complains, tsd-lite will complain in the same way. EDIT: Only that tsd-lite compiles in memory and does not emit anything, so these extra emit options have no impact.
There was a problem hiding this comment.
Thanks for the explanation Tom, makes sense.
The TS docs lack a little bit on composite and incremental - but it certainly seems to behave the way you're describing. I'll close the issue on the jest runner side :)
There was a problem hiding this comment.
Wait. I just realised that tsc needs -b flag to work with composites, not -p. So error comes from something else. Hm.. Just tried to use yarn tsc -b src/__typetests__ with previous experiment. Same result.
Back to your question, tsd-lite looks for a tsconfig.json nearest to a test file. If not found, it will use defaults of the compiler. If you delete __typetests__/tsconfig.json, tsd-lite will use config from packages/router. It gives the same error, adding {"composite": false} to packages/router/tsconfig.json makes it work.
Very interesting case. I have to dig deeper into this to understand. In any case, since tsc throws the same error this must be a config issue.
There was a problem hiding this comment.
Can’t stop ;D I just looked at Jest repo where tsd-lite is used (with 1200+ assertions passing) and in __typetests__/tsconfig.json we have "noUnusedLocals": false, "noUnusedParameters": false, etc. In a larger test suite these are useful. With time it becomes handy to have dedicated tsconfig.json for type tests, although it might look strange at first.
There was a problem hiding this comment.
I mean to ask a much simpler question, why doesn't tsd-lite work with composite? Just for knowledge - I don't quite grasp what the composite flag is for (outside of a build-time optimisation). I wouldn't be surprised if it's just a misconfiguration on our side.
Thank you very much for looking into it - glad it's piqued your interest!
There was a problem hiding this comment.
Oh my - I think I spotted the problem! Look at the last exclude glob, in the output for the tsc config it ignores the test files 😅
There was a problem hiding this comment.
Right. I was looking at that glob. Might be the cause, but note that routeParamsTypes.ts is reported as not included, which is not a test file. Also you need those globs for build. So they have to stay.
I think tsd-lite should work with composites, but with __typetests__/tsconfig.json it needs references. Just like tsc in my experiment, which I described above. At the same time, composite is useful for building libraries, there is not much use of it in testing. Perhaps there is a use case, but I think "composite": false is better solution in your case.
Import type file directly
| @@ -0,0 +1,3 @@ | |||
| { | |||
| "extends": "../../tsconfig.json" | |||
There was a problem hiding this comment.
Yes, the file is included. I see the same config from inside tsd-lite. It is using TS compiler’s APIs to find and read config.
The error we saw comes from TS compiler and it looks misleading. Here is an experiment:
- Add empty
tsconfig.jsonto__typetests__folder. - Run
yarn tsc -p src/__typetests__. All works. - Add
"compilerOptions": {"composite": true}to the sametsconfig.jsonand run the same command. - Oh.. that’s the error we saw.. And it came from
tscthis time. - Now also add
"references": [{ "path": "../../" }]to the sametsconfig.jsonand run the command.
All works again. Isn’t it that with "composite": true each directory with tsconfig.json turns into a project? So in this case the compiler complains, because it sees src and __typetests__ as separate projects.
By the way, tsd-lite wraps TS compilers’s APIs to typecheck and compare types. If tsc complains, tsd-lite will complain in the same way. EDIT: Only that tsd-lite compiles in memory and does not emit anything, so these extra emit options have no impact.
Co-authored-by: Tom Mrazauskas <[email protected]>
Both of those are allowed, and supported. I added a couple of (regular) tests to make it more explicit |
|
👏 |
|
Hello @SimenB good to see you around here :) |
|
Love seeing |
It's a great project, although a little hard to search for (because testing typescript just returns a bunch of articles how to configure jest for ts!) One thing that feels a little But I also understand why it's this way :) |
That’s a limitation of test("is ExpectedType?", () => {
expect<SomeType>().type.toBe<ExpectedType>();
});
test("is typeof expectedExpression?", () => {
expect(receivedExpression).type.toBe(expectedExpression);
});And everything in between ;D Actually the above example is already fully working. I just have to cleaning up TODOs and to write more docs. Moving forward slowly. If that makes sense, I will ping you after releasing this beauty (; Or just follow: jest-community/jest-runner-tsd#32 |
|
Following! This is exactly what I was hoping the syntax would be. I’m a big fan of tests reading like documentation, and this is definitely a step in the right direction. Thank you both for your hard work on this (and jest) 👍👍👍👍 |
This PR uses https://github.com/jest-community/jest-runner-tsd to add type tests
This is helpful because we currently have no way of checking complicated mapper types, apart from manually generating a test project, writing some code, and running tsc (or waiting for red)
Todo:
- [ ] Add tests forWill do in another PRMergePrismaWithSdlTypesCloses #6346
Closes #6319