-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
export types.BaseNode for generic utility use #12600
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 55e1a6b:
|
Does |
It didn't when I tried. I think it's because |
Is that code public? I have nothing against exposing |
Yeah, mauve I did something wrong, but using Node here didn't work: https://github.com/kentcdodds/babel-plugin-codegen/blob/pr/migrate-to-ts/src/helpers.ts#L85 |
Ok I see the error:
I tried to export
I think it has to do with covariance/contravariance of type parameters (I don't know exactly what it means), but I fixed the type errors like this: diff --git a/src/helpers.ts b/src/helpers.ts
index c813b8f..0701b03 100644
--- a/src/helpers.ts
+++ b/src/helpers.ts
@@ -63,9 +63,9 @@ function getReplacement(
})()
}
-function applyReplacementToPath(
+function applyReplacementToPath<SpecificNode extends babelCore.types.Node>(
replacement: babelCore.Node | Array<babelCore.Node> | null | undefined,
- path: babelCore.NodePath,
+ path: babelCore.NodePath<SpecificNode>,
) {
if (replacement) {
// If it's not an array, wrap into an array
@@ -79,16 +79,16 @@ function applyReplacementToPath(
}
}
-type ReplaceOptions = {
+type ReplaceOptions<SpecificNode extends babelCore.types.Node> = {
// https://github.com/babel/babel/pull/12600
// path: babelCore.NodePath<babelCore.types.BaseNode>
- path: babelCore.NodePath<any>
+ path: babelCore.NodePath<SpecificNode>
code: string | Buffer
fileOpts: babelCore.TransformOptions
args?: Array<any>
}
-function replace(
- {path, code, fileOpts, args}: ReplaceOptions,
+function replace<SpecificNode extends babelCore.types.Node>(
+ {path, code, fileOpts, args}: ReplaceOptions<SpecificNode>,
babel: typeof babelCore,
) {
const replacement = getReplacement({code, args, fileOpts}, babel) |
That worked. Thank you for taking the time @nicolo-ribaudo! Cheers :) |
I'm trying to write a utility that accepts a
NodePath
of anyBaseNode
type and does stuff with it (replaces etc.). So I need to type that utility withBaseNode
, but that's not exported yet so this simply exports theBaseNode
.