Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix type declarations for typescript <4 #251

Merged
merged 2 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>;