From d25a89af817bf9f46fc9a9d2cf730baabac4b525 Mon Sep 17 00:00:00 2001 From: mmkal Date: Wed, 25 Nov 2020 11:10:16 -0500 Subject: [PATCH 1/2] Fix type declarations for typescript <4 --- package.json | 1 + src/node_redis/multi.ts | 11 +---------- src/type-util.ts | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 src/type-util.ts diff --git a/package.json b/package.json index cac38d28..d643d6f4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/node_redis/multi.ts b/src/node_redis/multi.ts index 75bc1d6f..39cd7a7b 100644 --- a/src/node_redis/multi.ts +++ b/src/node_redis/multi.ts @@ -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, B]; -type VariadicTuplesPrefixesSupported = Push_ts4<[1, 2], 3> extends { length: 3 } ? "yes" : "no"; -type Push = VariadicTuplesPrefixesSupported extends "yes" - ? Push_ts4 - : Array; +import { Push } from "../type-util"; export type ResultType = ReturnType extends Promise ? X : never; diff --git a/src/type-util.ts b/src/type-util.ts new file mode 100644 index 00000000..43fb0915 --- /dev/null +++ b/src/type-util.ts @@ -0,0 +1,14 @@ +// 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, 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 = VariadicTuplesPrefixesSupported extends "yes" + ? Push_ts4 + : Array; From c28c4b1ef9944677b240632c35d06a56f700a40a Mon Sep 17 00:00:00 2001 From: mmkal Date: Wed, 25 Nov 2020 11:16:37 -0500 Subject: [PATCH 2/2] Add postbuild comment --- src/type-util.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/type-util.ts b/src/type-util.ts index 43fb0915..08b8a069 100644 --- a/src/type-util.ts +++ b/src/type-util.ts @@ -1,5 +1,6 @@ -// 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 +// 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, B];