Skip to content

Appends the allowed object keys in typescript#2502

Merged
hueniverse merged 1 commit intohapijs:masterfrom
web4ken:master
Feb 8, 2021
Merged

Appends the allowed object keys in typescript#2502
hueniverse merged 1 commit intohapijs:masterfrom
web4ken:master

Conversation

@web4ken
Copy link
Contributor

@web4ken web4ken 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?

@web4ken
Copy link
Contributor Author

web4ken commented Jan 18, 2021

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