Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/61056 |
| | [PresetTarget, object] | ||
| | [PresetTarget, object, string]; | ||
| | PresetTarget<Option> | ||
| | [PresetTarget<Option>, Partial<Option>] |
There was a problem hiding this comment.
This change would allow users to explicitly type check preset options: For example,
import babelPresetEnv from "@babel/preset-env";
import type { Options as BabelPresetEnvOptions } from "@babel/preset-env";
import type { PresetTarget } from "@babel/core";
export default {
presets: [
// TS will throw `foo` is not defined in BabelPresetEnvOptions
[babelPresetEnv, { targets: "chrome 123", foo: "string" }] satisifies PresetTarget<BabelPresetEnvOptions>
]
} But I didn't figure out how to make it an implicit constraint.
There was a problem hiding this comment.
Would something like
export default {
presets: [
// TS will throw `foo` is not defined in BabelPresetEnvOptions
[babelPresetEnv, { targets: "chrome 123", foo: "string" }]
]
} as BabelConfigwork, or not because TS cannot infer from babelPresetEnv to the second element of the list?
There was a problem hiding this comment.
AFAIK TS calculates the constraints for the generics only when it's a type parameter. So it would require a function call.
import * as babel from '@babel/core';
function p<Option>(p: babel.PresetTarget<Option>, o: Partial<Option>) {
return [p, o];
}
babel.transformSync('', {
p(babelPresetEnv, {
bugfixes: true,
modules: false,
// input suggestion works here
}),
],
});I guess the input suggestion doesn't work with transformSync because type InputOptions has plugins?: PluginItem[], which is the same as plugins?: PluginItem<object>[].
There was a problem hiding this comment.
This is a very good point, we could consider exporting two type-aware utilities pluginWithOptions, presetWithOptions such that the plugin/preset option can be properly type checked.
@nicolo-ribaudo @liuxingbaoyu WDYT?
|
commit: |
| | [PresetTarget, object] | ||
| | [PresetTarget, object, string]; | ||
| | PresetTarget<Option> | ||
| | [PresetTarget<Option>, Partial<Option>] |
There was a problem hiding this comment.
Why are the Options partial?
There was a problem hiding this comment.
Good question, this is to workaround the current preset-env option definition:
babel/packages/babel-preset-env/src/types.d.ts
Lines 24 to 40 in ad32683
The options here should have marked every property as optional, but the non-optional one is also handy to type the normalized options.
There was a problem hiding this comment.
Could we move Partial<...> to the preset-env export in the index file, rather than just re-exporting Options as-is?
It'd be great to be able to get type errors when required options are missing.
|
I've tested the types on stackblitz and is working for me as well 👍 |
81e3d00 to
e88896f
Compare
/cc @sapphi-red.