Skip to content

Commit

Permalink
Fix type declarations for typescript <4 (#251)
Browse files Browse the repository at this point in the history
* Fix type declarations for typescript <4

* Add postbuild comment
  • Loading branch information
mmkal committed Dec 3, 2020
1 parent f356352 commit 98a0ba7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"_": "echo \"these pass-through-to-docker scripts are mostly just as a hint for when I come back to this in a few weeks and forget what I'm supposed to do.\"",
"prebuild": "yarn codegen",
"build": "yarn typecheck && yarn compile",
"postbuild": "sed -i 's~declare type Push_ts4~// @ts-ignore (https://github.com/mmkal/handy-redis/pulls/251)\\ndeclare type Push_ts4~g' dist/type-util.d.ts",
"check-clean": "check-clean",
"ci": "run-s clean build coverage lint check-clean",
"preclean": "del-cli temp/backup-test-generated && mkdir -p temp && cp -r test/generated temp/backup-test-generated",
Expand Down
11 changes: 1 addition & 10 deletions src/node_redis/multi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ import * as nodeRedis from "redis";
import { flattenDeep } from "../flatten";
import { Commands } from "../generated/interface";
import { promisify } from "util";

// Variadic tuple prefixes only work in ts>4. To support lower typescript versions, check if the feature works
// this means we need ts-ignore, not ts-expect-error because it's _not_ an error in ts>4
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
type Push_ts4<A extends unknown[], B> = [...A, B];
type VariadicTuplesPrefixesSupported = Push_ts4<[1, 2], 3> extends { length: 3 } ? "yes" : "no";
type Push<A extends unknown[], B> = VariadicTuplesPrefixesSupported extends "yes"
? Push_ts4<A, B>
: Array<A[number] | B>;
import { Push } from "../type-util";

export type ResultType<K extends keyof Commands> = ReturnType<Commands[K]> extends Promise<infer X> ? X : never;

Expand Down
15 changes: 15 additions & 0 deletions src/type-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Rest elements must be last in tuple types prior to typescript 4.x. To support lower typescript versions, check if the feature works
// this means we need ts-ignore, not ts-expect-error because it's _not_ an error in ts>=4
// Note: this ts-ignore doesn't get emitted to d.ts, so it needs to be re-added in "postbuild" (see package.json)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
type Push_ts4<A extends unknown[], B> = [...A, B];
type VariadicTuplesPrefixesSupported = Push_ts4<[1, 2], 3> extends [1, 2, 3] ? "yes" : "no";

/**
* @example
* Push<[1, 2], 3> // resolves to [1, 2, 3] for typescript>=4, or (1 | 2 | 3)[] for typescript<4
*/
export type Push<A extends unknown[], B> = VariadicTuplesPrefixesSupported extends "yes"
? Push_ts4<A, B>
: Array<A[number] | B>;

0 comments on commit 98a0ba7

Please sign in to comment.