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

Appends the allowed object keys in typescript #2502

Merged
merged 1 commit into from
Feb 8, 2021
Merged

Appends the allowed object keys in typescript #2502

merged 1 commit into from
Feb 8, 2021

Conversation

mr-kenikh
Copy link
Contributor

@mr-kenikh mr-kenikh commented Nov 1, 2020

If you try to appends a strongly typed object schema

import Joi from 'joi'

type TSchema = {
  a: Joi.StringSchema
  b: Joi.NumberSchema
}

const schema = Joi.object<TSchema>({
  a: Joi.string(),
  b: Joi.number(),
})

const schemaExtended = schema.append({
  c: Joi.boolean(),
})

it result in error

Argument of type '{ a: Joi.StringSchema; b: Joi.NumberSchema; c: Joi.BooleanSchema; }' is not assignable to parameter of type 'SchemaMap<TSchema>'.
Object literal may only specify known properties, and 'c' does not exist in type 'SchemaMap<TSchema>'.

I suggested a commit that would make possible to do that:

import Joi from 'joi'

type TSchema = {
  a: Joi.StringSchema
  b: Joi.NumberSchema
}

type TSchemaExtended = TSchema & {
  c: Joi.BooleanSchema
}

const schema = Joi.object<TSchema>({
  a: Joi.string(),
  b: Joi.number(),
})

const schemaExtended = schema.append<TSchemaExtended>({
  c: Joi.boolean(),
})

@hueniverse
Copy link
Contributor

Thanks! Can you add this to the TS test file?

@mr-kenikh
Copy link
Contributor Author

Tests already exists, this commit fix:

  • IDE (like Visual Studio Code) error highlighting
  • and TypeScript compile error

@hueniverse hueniverse merged commit b050427 into hapijs:master Feb 8, 2021
@hueniverse hueniverse self-assigned this Feb 8, 2021
@hueniverse hueniverse added bug Bug or defect types TypeScript type definitions labels Feb 8, 2021
@hueniverse hueniverse added this to the 17.4.0 milestone Feb 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug or defect types TypeScript type definitions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants