Skip to content

Commit

Permalink
fix: MaybePromiseDeep case of null with .then (graphql-nexus#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Kuhrt authored Aug 3, 2020
1 parent fe5fea7 commit 61eccca
Show file tree
Hide file tree
Showing 9 changed files with 606 additions and 31 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@
"release:pr": "dripip pr",
"release:preview": "dripip preview",
"release:stable": "dripip stable",
"test": "jest",
"test:ci": "jest --coverage --maxWorkers 2",
"test": "yarn test:types && jest",
"test:ci": "yarn test:types && jest --coverage --maxWorkers 2",
"test:debug": "node --inspect-brk $(yarn bin)/jest -i --watch",
"test:types": "tsc -p tsconfig.spec.types.json",
"ts-ast-reader": "cd examples/ts-ast-reader && yarn start",
"unlink-examples": "cd website && yarn && yarn gulp unlink-examples",
"upgrade-deps": "cd website && yarn && yarn gulp upgrade-deps",
Expand Down Expand Up @@ -95,6 +96,7 @@
"sort-package-json": "^1.22.1",
"ts-jest": "^26.0.0",
"ts-node": "^7.0.1",
"tsd": "^0.13.1",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.0",
"typescript": "^3.9.6"
Expand Down
19 changes: 19 additions & 0 deletions src/typegenTypeHelpers.spec.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expectAssignable } from 'tsd'
import { MaybePromiseDeep } from './typegenTypeHelpers'

/**
* MaybePromiseDeep
*/

// A case found by Sytten https://github.com/graphql-nexus/schema/issues/470
// The presence of .then was leading to type errors
type Ones = 1[]
type GraphQLResponse = Ones | null
const getOnes = () => Promise.resolve([] as Ones)
expectAssignable<MaybePromiseDeep<GraphQLResponse>>(getOnes().then((ones) => ones))
// object with array
expectAssignable<MaybePromiseDeep<{ a: 1[] }>>({ a: [1] })
expectAssignable<MaybePromiseDeep<{ a: 1[] }>>(Promise.resolve({ a: [1] }))
// array
expectAssignable<MaybePromiseDeep<1[]>>([1])
expectAssignable<MaybePromiseDeep<1[]>>(Promise.resolve([1]))
12 changes: 7 additions & 5 deletions src/typegenTypeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,24 @@ export type MaybePromise<T> = PromiseLike<T> | T
* to help signify that.
*/
export type MaybePromiseDeep<T> = Date extends T
? MaybePromise<T>
: null extends T
? MaybePromise<T>
: boolean extends T
? MaybePromise<T>
: number extends T
? MaybePromise<T>
: string extends T
? MaybePromise<T>
: T extends Array<infer U>
? MaybePromise<Array<MaybePromiseDeep<U>>>
: T extends ReadonlyArray<infer Y>
? MaybePromise<ReadonlyArray<MaybePromiseDeep<Y>>>
: T extends object
? MaybePromise<
| T
| {
[P in keyof T]: T[P] extends Array<infer U>
? MaybePromise<Array<MaybePromiseDeep<U>>>
: T[P] extends ReadonlyArray<infer Y>
? MaybePromise<ReadonlyArray<MaybePromiseDeep<Y>>>
: MaybePromiseDeep<T[P]>
[P in keyof T]: MaybePromiseDeep<T[P]>
}
>
: MaybePromise<T>
Expand Down
20 changes: 20 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"outDir": "dist",
"declaration": true,
// "declarationMap": true,
"sourceMap": true
},
"exclude": [
"./**/*.spec.types.ts",
"./examples",
"./scripts",
"./dist",
"./tests",
"./website",
"./gulpfile.ts",
"./package.json"
]
}
2 changes: 1 addition & 1 deletion tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "./tsconfig.json",
"extends": "./tsconfig.build.json",
"compilerOptions": {
"module": "CommonJS",
"outDir": "dist"
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "./tsconfig.json",
"extends": "./tsconfig.build.json",
"compilerOptions": {
"moduleResolution": "node",
"module": "ES2015",
Expand Down
15 changes: 2 additions & 13 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,8 @@
"rootDir": "src",
"strict": true,
"noUnusedLocals": true,
"declaration": true,
// "declarationMap": true,
"importHelpers": true,
"resolveJsonModule": true,
"sourceMap": true
"resolveJsonModule": true
},
"exclude": [
"./examples",
"./scripts",
"./dist",
"./tests",
"./website",
"./gulpfile.ts",
"./package.json"
]
"exclude": ["./examples", "./scripts", "./dist", "./tests", "./website", "./gulpfile.ts", "./package.json"]
}
7 changes: 7 additions & 0 deletions tsconfig.spec.types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": true
},
"include": ["./**/*.spec.types.ts"]
}
Loading

0 comments on commit 61eccca

Please sign in to comment.