Skip to content

Commit

Permalink
Restore existing type inference behaviour of z.ZodObject (colinhacks#…
Browse files Browse the repository at this point in the history
…1122)

* Fix addQuestionsMarks so it resolves correctly in more cases

Fixes this issue: colinhacks#1121

Specifically, `{ property: (T | undefined) & T }` does not seem to
flatten to `{ property: T }` in the case that T is a complex string literal.

* Ran lint on languageServerFeatures.test.ts
  • Loading branch information
bentefay authored and MrAwesome committed May 20, 2022
1 parent d915289 commit 3df873b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
13 changes: 9 additions & 4 deletions deno/lib/types.ts
Expand Up @@ -1418,13 +1418,18 @@ export namespace objectUtil {
[k in Exclude<keyof U, keyof V>]: U[k];
} & V;

export type requiredKeys<T extends object> = {
type optionalKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? k : never;
}[keyof T];

type requiredKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? never : k;
}[keyof T];

export type addQuestionMarks<T extends object> = {
[k in keyof T]?: T[k];
} & { [k in requiredKeys<T>]: T[k] };
export type addQuestionMarks<T extends object> = Partial<
Pick<T, optionalKeys<T>>
> &
Pick<T, requiredKeys<T>>;

export type identity<T> = T;
export type flatten<T extends object> = identity<{ [k in keyof T]: T[k] }>;
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/languageServerFeatures.test.ts
@@ -1,8 +1,9 @@
// @ts-ignore TS6133
import { expect, fit } from "@jest/globals";
import { filePath } from "./languageServerFeatures.source";
import { Project, Node, SyntaxKind } from "ts-morph";
import path from "path";
import { Node, Project, SyntaxKind } from "ts-morph";

import { filePath } from "./languageServerFeatures.source";

// The following tool is helpful for understanding the TypeScript AST associated with these tests:
// https://ts-ast-viewer.com/ (just copy the contents of languageServerFeatures.source into the viewer)
Expand Down
13 changes: 9 additions & 4 deletions src/types.ts
Expand Up @@ -1374,13 +1374,18 @@ export namespace objectUtil {
[k in Exclude<keyof U, keyof V>]: U[k];
} & V;

export type requiredKeys<T extends object> = {
type optionalKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? k : never;
}[keyof T];

type requiredKeys<T extends object> = {
[k in keyof T]: undefined extends T[k] ? never : k;
}[keyof T];

export type addQuestionMarks<T extends object> = {
[k in keyof T]?: T[k];
} & { [k in requiredKeys<T>]: T[k] };
export type addQuestionMarks<T extends object> = Partial<
Pick<T, optionalKeys<T>>
> &
Pick<T, requiredKeys<T>>;

export type identity<T> = T;
export type flatten<T extends object> = identity<{ [k in keyof T]: T[k] }>;
Expand Down

0 comments on commit 3df873b

Please sign in to comment.