From 825cc01f3bf136322b814152ede8fe0d4a322019 Mon Sep 17 00:00:00 2001 From: Giulio Caprino Date: Fri, 18 Sep 2020 17:40:18 +0100 Subject: [PATCH] fix(typescript): restore support for typescript 3 --- definitelyTypedTests/package-lock.json | 6 +++--- definitelyTypedTests/package.json | 6 +++--- package-lock.json | 6 +++--- package.json | 2 +- src/transformer/descriptor/tuple/tuple.ts | 17 ++++++++++++++++- src/transformer/mockDefiner/mockDefiner.ts | 5 ++--- 6 files changed, 28 insertions(+), 14 deletions(-) diff --git a/definitelyTypedTests/package-lock.json b/definitelyTypedTests/package-lock.json index a70d281e6..91fd186e8 100644 --- a/definitelyTypedTests/package-lock.json +++ b/definitelyTypedTests/package-lock.json @@ -1125,9 +1125,9 @@ } }, "typescript": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.0.2.tgz", - "integrity": "sha512-e4ERvRV2wb+rRZ/IQeb3jm2VxBsirQLpQhdxplZ2MEzGvDkkMmPglecnNDfSUBivMjP93vRbngYYDQqQ/78bcQ==" + "version": "3.9.7", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.7.tgz", + "integrity": "sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==" }, "upath": { "version": "1.2.0", diff --git a/definitelyTypedTests/package.json b/definitelyTypedTests/package.json index 5ab470870..55c1bf937 100644 --- a/definitelyTypedTests/package.json +++ b/definitelyTypedTests/package.json @@ -16,10 +16,10 @@ "author": "", "license": "ISC", "dependencies": { + "cross-var": "^1.1.0", "dotenv": "^8.2.0", "ttypescript": "^1.5.10", - "typescript": "^4.0.2", - "upath": "^1.2.0", - "cross-var": "^1.1.0" + "typescript": "^3.9.7", + "upath": "^1.2.0" } } diff --git a/package-lock.json b/package-lock.json index cb51491c0..f3292930a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14634,9 +14634,9 @@ "dev": true }, "typescript": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.0.2.tgz", - "integrity": "sha512-e4ERvRV2wb+rRZ/IQeb3jm2VxBsirQLpQhdxplZ2MEzGvDkkMmPglecnNDfSUBivMjP93vRbngYYDQqQ/78bcQ==", + "version": "3.9.7", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.7.tgz", + "integrity": "sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==", "dev": true }, "uglify-js": { diff --git a/package.json b/package.json index 883b0d1b0..99860d676 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,7 @@ "ts-node": "^9.0.0", "tsconfig-paths": "^3.9.0", "ttypescript": "1.5.10", - "typescript": "^4.0.2", + "typescript": "^3.9.7", "webpack": "^4.44.1", "webpack-cli": "^3.3.12", "webpack-merge": "^5.1.2", diff --git a/src/transformer/descriptor/tuple/tuple.ts b/src/transformer/descriptor/tuple/tuple.ts index 8f1ba8d8b..4f1f4fbc1 100644 --- a/src/transformer/descriptor/tuple/tuple.ts +++ b/src/transformer/descriptor/tuple/tuple.ts @@ -7,6 +7,21 @@ export function GetTupleDescriptor( scope: Scope ): ts.Expression { return ts.createArrayLiteral( - node.elements.map((e: ts.TypeNode) => GetDescriptor(e, scope)) + getDescriptorFromTypeNodes( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ((node as any).elementTypes as ts.NodeArray) || + // Typescript 4 doesn't have elementTypes but it does have elements, to support both versions + // this casting is needed. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ((node as any).elements as ts.NodeArray), + scope + ) ); } + +function getDescriptorFromTypeNodes( + nodes: ts.NodeArray, + scope: Scope +): ts.Expression[] { + return nodes.map((e: ts.TypeNode) => GetDescriptor(e, scope)); +} diff --git a/src/transformer/mockDefiner/mockDefiner.ts b/src/transformer/mockDefiner/mockDefiner.ts index f59cb17c1..38dd5fd3b 100644 --- a/src/transformer/mockDefiner/mockDefiner.ts +++ b/src/transformer/mockDefiner/mockDefiner.ts @@ -182,9 +182,8 @@ export class MockDefiner { ); } - // NOTE: TypeScript does not support inference through has/get, but we know - // for a fact that the result here is a string! - return this._declarationCache.get(declaration) as string; + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return this._declarationCache.get(declaration)!; } public registerMockFor(