-
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
kbn/config-schema: Consider maybe properties as optional keys in ObjectType #63838
Changes from 1 commit
3c28c21
58e2756
996cd13
c229590
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 |
---|---|---|
|
@@ -26,9 +26,26 @@ export type Props = Record<string, Type<any>>; | |
|
||
export type TypeOf<RT extends Type<any>> = RT['type']; | ||
|
||
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] | ||
>; | ||
|
||
// Because of https://github.com/Microsoft/TypeScript/issues/14041 | ||
// this might not have perfect _rendering_ output, but it will be typed. | ||
export type ObjectResultType<P extends Props> = Readonly<{ [K in keyof P]: TypeOf<P[K]> }>; | ||
export type ObjectResultType<P extends Props> = Readonly< | ||
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. 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. Yea, TS magic... TBH I really thought there would be an easier way to type that, but AFAIK there is not, as the |
||
{ [K in keyof OptionalProperties<P>]?: TypeOf<P[K]> } & | ||
{ [K in keyof RequiredProperties<P>]: TypeOf<P[K]> } | ||
>; | ||
|
||
interface UnknownOptions { | ||
/** | ||
|
@@ -41,7 +58,8 @@ interface UnknownOptions { | |
} | ||
|
||
export type ObjectTypeOptions<P extends Props = any> = TypeOptions< | ||
{ [K in keyof P]: TypeOf<P[K]> } | ||
{ [K in keyof OptionalProperties<P>]?: TypeOf<P[K]> } & | ||
{ [K in keyof RequiredProperties<P>]: TypeOf<P[K]> } | ||
> & | ||
UnknownOptions; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,23 +170,23 @@ export class RouteValidator<P = {}, Q = {}, B = {}> { | |
* @internal | ||
*/ | ||
public getParams(data: unknown, namespace?: string): Readonly<P> { | ||
return this.validate(this.config.params, this.options.unsafe?.params, data, namespace); | ||
return this.validate(this.config.params, this.options.unsafe?.params, data, namespace) as P; | ||
Comment on lines
-173
to
+180
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. So, introducing this change in the I think this is still fine because:
But if someone wants to take another look, please do 😄 |
||
} | ||
|
||
/** | ||
* Get validated query params | ||
* @internal | ||
*/ | ||
public getQuery(data: unknown, namespace?: string): Readonly<Q> { | ||
return this.validate(this.config.query, this.options.unsafe?.query, data, namespace); | ||
return this.validate(this.config.query, this.options.unsafe?.query, data, namespace) as Q; | ||
} | ||
|
||
/** | ||
* Get validated body | ||
* @internal | ||
*/ | ||
public getBody(data: unknown, namespace?: string): Readonly<B> { | ||
return this.validate(this.config.body, this.options.unsafe?.body, data, namespace); | ||
return this.validate(this.config.body, this.options.unsafe?.body, data, namespace) as B; | ||
} | ||
|
||
/** | ||
|
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.
Until we got proper way to test TS typings, this is the best test I could add. Not even sure it's very relevant, as I can only assert valid assignments and not invalid ones.
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.
I added this test to #53762 we can improve it later.