-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
config-schema incorrectly types a Maybe field as T | undefined
instead of optional on objects
#63725
Comments
Pinging @elastic/kibana-platform (Team:Platform) |
I think The type should be interface SchemaWithOptional {
prop?: string | undefined;
} However, technically, this requires the parent Will take a look. |
This all goes down to
Would need to find a TS way to check if |
So, after a few hours hating typescript: This can be fixed by changing type ObjectResultType<P extends Props> = Readonly<{ [K in keyof P]: TypeOf<P[K]> }>; with type OptionalProperties<Base extends Props> = Pick<
Base,
{
[Key in keyof Base]: undefined extends TypeOf<Base[Key]> ? Key : never;
}[keyof Base]
>;
type RequiredProperties<Base extends Props> = Pick<
Base,
{
[Key in keyof Base]: undefined extends TypeOf<Base[Key]> ? never : Key;
}[keyof Base]
>;
export type ObjectResultType<P extends Props> = Readonly<
{ [K in keyof OptionalProperties<P>]?: TypeOf<P[K]> } &
{ [K in keyof RequiredProperties<P>]: TypeOf<P[K]> }
>; The const type = schema.object({
required: schema.string(),
optional: schema.maybe(schema.string()),
});
type SchemaType = TypeOf<typeof type>;
let foo: SchemaType = { // no error
required: 'foo',
};
foo = { // no error
required: 'hello',
optional: undefined,
};
foo = { // no error
required: 'hello',
optional: 'bar',
};
foo = { // error
optional: 'bar',
};
foo = { // error
}; However this change causes the router validator ( This is directly related to this type kibana/src/core/server/http/router/validator/validator.ts Lines 83 to 90 in d8f94b1
However this file is a very sensible piece of TS magic, and I couldn't find a proper way to fix the typings without force casting everywhere. If someone wants to have a look, feel free. |
haha sorry if I broke you @pgayvallet |
Kibana version: 7.7+ (on master)
Describe the bug:
If you use
schema.maybe()
on an object , the prop is only marked asT | undefined
, but it should be optional (perhaps both, rather than instead).Steps to reproduce:
SchemaWithOptional
- theprop
field is marked asstring | undefined
, meaning you can't omit theprop
field and have to set it as undefined.Expected behavior:
The type should be:
or perhaps:
The text was updated successfully, but these errors were encountered: