Skip to content
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

Closed
gmmorris opened this issue Apr 16, 2020 · 5 comments · Fixed by #63838
Closed
Labels
bug Fixes for quality problems that affect the customer experience Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc

Comments

@gmmorris
Copy link
Contributor

gmmorris commented Apr 16, 2020

Kibana version: 7.7+ (on master)

Describe the bug:
If you use schema.maybe() on an object , the prop is only marked as T | undefined, but it should be optional (perhaps both, rather than instead).

Screenshot 2020-04-16 at 11 17 55

Screenshot 2020-04-16 at 11 18 14

Steps to reproduce:

  1. Define an object like so:
const schemaWithOptional = schema.object({
  prop: schema.maybe(schema.string()),
});
export type SchemaWithOptional = TypeOf<typeof schemaWithOptional>;
  1. Look at typescript signature of SchemaWithOptional - the prop field is marked as string | undefined, meaning you can't omit the prop field and have to set it as undefined.
interface SchemaWithOptional {
   prop: string | undefined;
}

Expected behavior:
The type should be:

interface SchemaWithOptional {
   prop?: string;
}

or perhaps:

interface SchemaWithOptional {
   prop?: string | undefined;
}
@gmmorris gmmorris added the Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc label Apr 16, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-platform (Team:Platform)

@pgayvallet
Copy link
Contributor

pgayvallet commented Apr 16, 2020

I think The type should be

interface SchemaWithOptional {
   prop?: string | undefined;
}

However, technically, this requires the parent object schema's Type to be dependant on the children schema types, which I'm not sure is doable in typescript.

Will take a look.

@pgayvallet
Copy link
Contributor

This all goes down to

export type ObjectResultType<P extends Props> = Readonly<{ [K in keyof P]: TypeOf<P[K]> }>;

Would need to find a TS way to check if TypeOf<P[K]> extends MaybeType

@pgayvallet
Copy link
Contributor

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 TypeOf is fixed:

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 (src/core/server/http/router/validator/validator.ts) to just implode, with

Screenshot 2020-04-17 at 00 02 23

This is directly related to this type

// Ugly as hell but we need this conditional typing to have proper type inference
type RouteValidationResultType<T extends RouteValidationSpec<any> | undefined> = NonNullable<
T extends RouteValidationFunction<any>
? ReturnType<T>['value']
: T extends Type<any>
? ReturnType<T['validate']>
: undefined
>;

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.

@pgayvallet pgayvallet added the bug Fixes for quality problems that affect the customer experience label Apr 17, 2020
@gmmorris
Copy link
Contributor Author

haha sorry if I broke you @pgayvallet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Fixes for quality problems that affect the customer experience Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants