Skip to content

Commit

Permalink
fix(types): default object type arg;
Browse files Browse the repository at this point in the history
- means dont have to expose `Properties` directly or user redeclare
  • Loading branch information
lukeed committed Aug 11, 2024
1 parent 8c6ec8d commit ac25ad7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
21 changes: 21 additions & 0 deletions infer.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,27 @@ assert<typeof O3>({
status: STRING,
});

// ---
// OBJECT / empty / any
// ---

let o4 = t.object();

type O4 = t.Infer<typeof o4>;
declare let O4: O4;

// deno-lint-ignore ban-types
assert<{}>(O4);
assert<Record<string, unknown>>(O4);

assert<O4>({
//
});

assert<O4>({
foo: 123,
});

// ---
// OBJECT / `required` match keys
// ---
Expand Down
10 changes: 8 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export type Type =
| _integer
| _null
| _number
| _object<Properties>
| _object
| _string
| _tuple<unknown>
| _all<unknown>
Expand Down Expand Up @@ -522,6 +522,12 @@ function _tuple<
} as _tuple<M>;
}

/**
* The {@link _object} property definitions.
*
* > [!NOTE]
* > You probably don't want this type, unless you are extending/building your own types.
*/
type Properties = {
[name: string]: Type & {
[OPTIONAL]?: true;
Expand All @@ -533,7 +539,7 @@ type Properties = {
*
* [Reference](https://json-schema.org/understanding-json-schema/reference/object)
*/
type _object<T extends Properties> = Annotations & {
type _object<T extends Properties = Properties> = Annotations & {
type: 'object';
properties?: T;
required?: (keyof T)[];
Expand Down

0 comments on commit ac25ad7

Please sign in to comment.