From 43de55e21e667e24cc804002c661f891fc7e179f Mon Sep 17 00:00:00 2001 From: Christoph Guttandin Date: Thu, 26 Mar 2020 23:41:58 +0100 Subject: [PATCH 1/2] Downlevel Omit when used as ExpressionWithTypeArguments --- baselines/ts3.4/test.d.ts | 3 +++ index.js | 22 ++++++++++++++++++++++ test/test.d.ts | 4 ++++ 3 files changed, 29 insertions(+) diff --git a/baselines/ts3.4/test.d.ts b/baselines/ts3.4/test.d.ts index afbd073..35eb474 100644 --- a/baselines/ts3.4/test.d.ts +++ b/baselines/ts3.4/test.d.ts @@ -27,3 +27,6 @@ export class G { export class H extends G { private "H.#private"; } +export interface I extends Pick> { + version: number; +} diff --git a/index.js b/index.js index ffedd05..ab480d5 100644 --- a/index.js +++ b/index.js @@ -123,6 +123,28 @@ function doTransform(checker, k) { return ts.createExportDeclaration(n.decorators, n.modifiers, n.exportClause, n.moduleSpecifier); } else if (ts.isImportClause(n) && n.isTypeOnly) { return ts.createImportClause(n.name, n.namedBindings); + } else if ( + ts.isExpressionWithTypeArguments(n) && + ts.isIdentifier(n.expression) && + n.expression.escapedText === "Omit" + ) { + const symbol = checker.getSymbolAtLocation(n.expression); + const typeArguments = n.typeArguments; + + if ( + symbol && + symbol.declarations.length && + symbol.declarations[0].getSourceFile().fileName.includes("node_modules/typescript/lib/lib") && + typeArguments + ) { + return ts.createTypeReferenceNode(ts.createIdentifier("Pick"), [ + typeArguments[0], + ts.createTypeReferenceNode(ts.createIdentifier("Exclude"), [ + ts.createTypeOperatorNode(typeArguments[0]), + typeArguments[1] + ]) + ]); + } } else if (ts.isTypeReferenceNode(n) && ts.isIdentifier(n.typeName) && n.typeName.escapedText === "Omit") { const symbol = checker.getSymbolAtLocation(n.typeName); const typeArguments = n.typeArguments; diff --git a/test/test.d.ts b/test/test.d.ts index a351f6c..992c37c 100644 --- a/test/test.d.ts +++ b/test/test.d.ts @@ -32,3 +32,7 @@ export class G { export class H extends G { #private } + +export interface I extends Omit { + version: number; +} From 60a399e4905d59d8a6b50b93489dfde582d1e069 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Mon, 3 Aug 2020 16:46:15 -0700 Subject: [PATCH 2/2] deduplicate code --- index.js | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index ab480d5..890f22a 100644 --- a/index.js +++ b/index.js @@ -124,29 +124,10 @@ function doTransform(checker, k) { } else if (ts.isImportClause(n) && n.isTypeOnly) { return ts.createImportClause(n.name, n.namedBindings); } else if ( - ts.isExpressionWithTypeArguments(n) && - ts.isIdentifier(n.expression) && - n.expression.escapedText === "Omit" + (ts.isTypeReferenceNode(n) && ts.isIdentifier(n.typeName) && n.typeName.escapedText === "Omit") || + (ts.isExpressionWithTypeArguments(n) && ts.isIdentifier(n.expression) && n.expression.escapedText === "Omit") ) { - const symbol = checker.getSymbolAtLocation(n.expression); - const typeArguments = n.typeArguments; - - if ( - symbol && - symbol.declarations.length && - symbol.declarations[0].getSourceFile().fileName.includes("node_modules/typescript/lib/lib") && - typeArguments - ) { - return ts.createTypeReferenceNode(ts.createIdentifier("Pick"), [ - typeArguments[0], - ts.createTypeReferenceNode(ts.createIdentifier("Exclude"), [ - ts.createTypeOperatorNode(typeArguments[0]), - typeArguments[1] - ]) - ]); - } - } else if (ts.isTypeReferenceNode(n) && ts.isIdentifier(n.typeName) && n.typeName.escapedText === "Omit") { - const symbol = checker.getSymbolAtLocation(n.typeName); + const symbol = checker.getSymbolAtLocation(ts.isTypeReferenceNode(n) ? n.typeName : n.expression); const typeArguments = n.typeArguments; if (