-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix TS 4.7 compatibility #3024
Fix TS 4.7 compatibility #3024
Conversation
@RebeccaStevens is this a required change for TypeScript 4.7 / Node.js 16 compatibility? I do want to use this feature to distribute ESM and CJS type definitions. I was thinking we'd look at that when 4.7 ships as part of AVA 5. (We can drop Node.js 12 support anyway.) |
From my testing so far, it seems to be required. I did read somewhere that top level exports should still grab the top level "types" field from package.json if there isn't a more specific one defined in the "exports" field. But from my testing this doesn't seem to be the case at the moment. (Maybe it will be fixed when 4.7 stable is released). |
See this comment for why I added Edit: This doesn't work as I expected. I'll probably revert the last commit but it seem like a bad idea to dupe all the type files. Maybe a build step would be wanted to this? Alternatively, it seem that if the |
index.d.cts
Outdated
@@ -0,0 +1,2 @@ | |||
export * from "."; | |||
export { default } from "."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this imply? Can we model the module.exports = test
behavior, as well as exporting the types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, I've gotten a bit better of an idea now as to how this new typing thing works. I'll update this pr soon to improve things.
1c23d43
to
39abeec
Compare
@novemberborn From my testing, this latest push I made seems to be working properly.
|
Seems like everything is good to go. |
@RebeccaStevens what do you think about making TS 4.7 the minimum version in AVA 5? I think that would make it possible to include some tests for these new file formats, no? I've tried some local files but I can't get
|
So in I haven't been following Ava 5's development so I can't really comment on it requiring TS 4.7+. However, it should still be possible to write tests for these new types without this requirement; the tests would just have to run in TS 4.7 environment. |
That's… surprising. Does
😞
There isn't any development as such. Dropping Node.js 12 requires a major bump, so once this is in AVA 4 it could be nice to upgrade to TS 4.7 as the recommended/tested setup. I've added the 4.7 RC to the test matrix. |
@novemberborn TS 4.7 has launched. Is there any blockers left for this? Also, with regards to Ava 5, I think it would be a good idea to up level all the code to esm and then down level to cjs with a build tool. The current approach of just defining esm that imports the cjs code can lead to a few issues. I could look into creating a PR for this if you like. Has any work been done specifically for Ava 5 yet? |
Could you elaborate?
No, it's just that removing Node.js 12 from the test matrix requires a major version bump, so we have an opportunity to set some new baselines. |
This is both tidier and ensures they're included in the package. Also rename the import in main.d.cts.
I can't figure this out. {
"name": "tmp.78i8xvt0sd",
"version": "1.0.0",
"devDependencies": {
"@sindresorhus/tsconfig": "^3.0.1",
"@types/node": "^16.11.36",
"ava": "github:RebeccaStevens/ava#node16-types-exports",
"typescript": "^4.7.2"
}
}
But for const test = require('ava')
test('pass', t => t.pass()) I get: > npx tsc --noEmit
test.cts:3:14 - error TS7006: Parameter 't' implicitly has an 'any' type.
3 test('pass', t => t.pass())
~
test.ts:3:14 - error TS7006: Parameter 't' implicitly has an 'any' type.
3 test('pass', t => t.pass())
~
Found 2 errors in 2 files.
Errors Files
1 test.cts:3
1 test.ts:3 It's fine for ESM, but CommonJS doesn't seem to be working with TS 4.7. |
Changing to
|
@sindresorhus what's your understanding of all this? I've also tried with the following import type {TestFn} from './test-fn.js';
/** Call to declare a test, or chain to declare hooks or test modifiers */
declare const test: TestFn;
/** Call to declare a test, or chain to declare hooks or test modifiers */
export = test; |
@novemberborn It looks like my attempt to reuse code has failed and we're going to need to duplicate the type files :( Oh well. I'll push a fix soon. |
b513102
to
7f58e5e
Compare
@novemberborn Push a fix, though I may have undone some of your commits. Feel free to remake them. |
With import type TestFn from "../index.js" assert { "resolution-mode": "import" };
declare const test: typeof TestFn;
export = test; |
I think so but we'll have to wait and see. |
Yes, I believe so. |
export {default} from '../lib/worker/main.cjs'; So some of the issue we are experiencing with this PR can happen with the actually JavaScript when mixing esm and cjs. There's an issue open in chai which uses this same approach. |
It's not working with
|
We're not considering any tools that compile the AVA code. This all seems to be working fine given that we have CJS and ESM entrypoints. |
It's working for me. Try |
It works fine at a surface level but it's kind of fragile. |
I think the type exports are only missing for the CJS files. Which makes sense because they're not exported there. I'm not sure how to combine a type export with the |
Same. As far as I know, it's not possible but maybe some one else can find a solution. In TS 4.8, the types will be able to be imported with a resolution mode assertion, thus allowing the types to be used in a cjs environment. But ideally we'd like to work without needing this. |
OK, so then we can't use A CJS target, using ESM syntax, works fine. But I suspect we then don't need any separate files. |
Hold that thought… |
@RebeccaStevens see my commits. Does this work all right for you? |
It's probably best to think about the |
I believe with |
No, I'm getting errors in cjs context. |
Ah yes that seems to work.
Could you share the specific syntax? My local test files seem fine 😕 |
We should be able to resolve the missing types in cjs based on this comment: microsoft/TypeScript#49299 (comment) |
Aha! Let's try that. Your CTS failure is because with my changes that now has to be |
Except… the test function implements an interface. I'm not sure we can merge that with a namespace. |
OK so we can do |
I'm inclined to leave it like this. We can look at exporting |
Thanks for your help with this @RebeccaStevens! |
See TS 4.7-rc Release Notes for details.