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
Improve babel-core typings #14622
Improve babel-core typings #14622
Changes from 13 commits
bbbc06b
4437ba2
2a882d4
dc3f872
352273d
89f08e7
e4db5f4
5f89ac8
72fa686
4929a87
e83294e
f5730b5
72e34b5
4b1e445
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import gensync from "gensync"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Todo (for myself): Continue reviewing from this file. |
||
import type { Gensync } from "gensync"; | ||
|
||
export type { | ||
ResolvedConfig, | ||
|
@@ -23,7 +24,7 @@ export type { | |
ValidatedOptions as PresetObject, | ||
} from "./validation/options"; | ||
|
||
import loadFullConfig from "./full"; | ||
import loadFullConfig, { type ResolvedConfig } from "./full"; | ||
import { loadPartialConfig as loadPartialConfigRunner } from "./partial"; | ||
|
||
export { loadFullConfig as default }; | ||
|
@@ -32,24 +33,28 @@ export type { PartialConfig } from "./partial"; | |
import { createConfigItem as createConfigItemImpl } from "./item"; | ||
import type { ConfigItem } from "./item"; | ||
|
||
const loadOptionsRunner = gensync<(opts: unknown) => any>(function* (opts) { | ||
const config = yield* loadFullConfig(opts); | ||
// NOTE: We want to return "null" explicitly, while ?. alone returns undefined | ||
return config?.options ?? null; | ||
}); | ||
const loadOptionsRunner = gensync<(opts: unknown) => ResolvedConfig | null>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Future note: we will be able to drop our type definitions for gensync and use DefinitelyTyped/DefinitelyTyped#60125 |
||
function* (opts) { | ||
const config = yield* loadFullConfig(opts); | ||
// NOTE: We want to return "null" explicitly, while ?. alone returns undefined | ||
return config?.options ?? null; | ||
}, | ||
); | ||
|
||
const createConfigItemRunner = | ||
gensync<(...args: Parameters<typeof createConfigItemImpl>) => ConfigItem>( | ||
createConfigItemImpl, | ||
); | ||
|
||
const maybeErrback = runner => (opts: unknown, callback?: Function) => { | ||
if (callback === undefined && typeof opts === "function") { | ||
callback = opts; | ||
opts = undefined; | ||
} | ||
return callback ? runner.errback(opts, callback) : runner.sync(opts); | ||
}; | ||
const maybeErrback = | ||
(runner: Gensync<(...args: any) => any>) => | ||
(opts: unknown, callback?: any) => { | ||
if (callback === undefined && typeof opts === "function") { | ||
callback = opts; | ||
opts = undefined; | ||
} | ||
return callback ? runner.errback(opts, callback) : runner.sync(opts); | ||
}; | ||
|
||
export const loadPartialConfig = maybeErrback(loadPartialConfigRunner); | ||
export const loadPartialConfigSync = loadPartialConfigRunner.sync; | ||
|
@@ -63,7 +68,7 @@ export const createConfigItemSync = createConfigItemRunner.sync; | |
export const createConfigItemAsync = createConfigItemRunner.async; | ||
export function createConfigItem( | ||
target: PluginTarget, | ||
options: any, | ||
options: Parameters<typeof createConfigItemImpl>[1], | ||
callback?: (err: Error, val: ConfigItem | null) => void, | ||
) { | ||
if (callback !== undefined) { | ||
|
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.
We can use
Parameters<T>[0]
andParameters<T>[1]
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.
It doesn't seem to be, it can't be passed into the Function.
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.
you should be able to use Parameters in here if you define proper correct type of
T
i'm not sure what should be in here, but as simple example you can do something like this:
see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-6.html