hapijs / hapijs/joi

Types of `object` are incorrect

Open
#2,978 0 comments 0 reactions 0 assignees View on GitHub
support
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

#### Support plan

* *is this issue currently blocking your project?* (yes):
* *is this issue affecting a production system?* (no):

#### Context

* *node version*: 18
* *module version with issue*: 17.9.2
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): standalone
* *any other relevant information*:

#### What are you trying to achieve or the steps to reproduce?

```js
const portSchema = Joi.number().integer().greater(2999).positive().required();
const hostSchema = Joi.alternatives([Joi.string().ip(), Joi.string().domain().allow('localhost')]).required();

const envSchema = Joi.object({
NODE_ENV: Joi.string().valid('production', 'development', 'test').required(),
PORT: portSchema.default(3000).optional(),
SERVICE_HOST: hostSchema,
REDIS_PORT: portSchema,
REDIS_HOST: hostSchema,
});
```

#### What was the result you got?

envSchema gets a type of `Joi.ObjectSchema` instead of specific object,
![image](https://github.com/hapijs/joi/assets/9304194/6a43b67f-5fb5-4d7d-b2cd-c2b6e711ae96)

#### What result did you expect?
It should contain reflect the object type...

Maybe we can use the types from https://github.com/maghis/types-joi/blob/master/index.d.ts which do work

I've managed to change types to reflect object's keys by modifying this
```ts
object(schema?: SchemaMap): ObjectSchema;
```
into
```ts
object(schema?: SchemaMap): ObjectSchema;
```
![image](https://github.com/hapijs/joi/assets/9304194/6df5f41d-eb08-4538-a0f8-2f61e133f4a2)

But it sets `unknown` to each key.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.