diff --git a/packages/transformers/typescript-types/src/shake.js b/packages/transformers/typescript-types/src/shake.js index 4bab766885f..6e54366ffb2 100644 --- a/packages/transformers/typescript-types/src/shake.js +++ b/packages/transformers/typescript-types/src/shake.js @@ -4,7 +4,7 @@ import type {TSModuleGraph} from './TSModuleGraph'; import ts from 'typescript'; import nullthrows from 'nullthrows'; -import {getExportedName, isDeclaration} from './utils'; +import {getExportedName, isDeclaration, createImportSpecifier} from './utils'; export function shake( moduleGraph: TSModuleGraph, @@ -238,7 +238,8 @@ function generateImports(moduleGraph: TSModuleGraph) { ); } else { namedSpecifiers.push( - ts.createImportSpecifier( + createImportSpecifier( + ts, name === imported ? undefined : ts.createIdentifier(imported), ts.createIdentifier(name), ), diff --git a/packages/transformers/typescript-types/src/utils.js b/packages/transformers/typescript-types/src/utils.js index 7178997f3a2..326f9f9dabb 100644 --- a/packages/transformers/typescript-types/src/utils.js +++ b/packages/transformers/typescript-types/src/utils.js @@ -1,5 +1,6 @@ // @flow import typeof TypeScriptModule from 'typescript'; // eslint-disable-line import/no-extraneous-dependencies +import type {Identifier, ImportSpecifier} from 'typescript'; export function getExportedName(ts: TypeScriptModule, node: any): ?string { if (!node.modifiers) { @@ -26,3 +27,22 @@ export function isDeclaration(ts: TypeScriptModule, node: any): boolean { ts.isTypeAliasDeclaration(node) ); } + +export function createImportSpecifier( + ts: TypeScriptModule, + propertyName: Identifier | void, + name: Identifier, + isTypeOnly: boolean = false, +): ImportSpecifier { + const [majorVersion, minorVersion] = ts.versionMajorMinor + .split('.') + .map(num => parseInt(num, 10)); + // The signature of createImportSpecifier had a breaking change in Typescript 4.5. + // see: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#type-modifiers-on-import-names + if (majorVersion > 4 || (majorVersion === 4 && minorVersion >= 5)) { + // $FlowFixMe + return ts.createImportSpecifier(isTypeOnly, propertyName, name); + } else { + return ts.createImportSpecifier(propertyName, name); + } +} diff --git a/yarn.lock b/yarn.lock index 93d89d24a1c..40acf9d1304 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13024,9 +13024,9 @@ typedarray@^0.0.6: integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= typescript@>=3.0.0: - version "4.2.4" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.4.tgz#8610b59747de028fda898a8aef0e103f156d0961" - integrity sha512-V+evlYHZnQkaz8TRBuxTA92yZBPotr5H+WhQ7bD3hZUndx5tGOa1fuCgeSjxAzM1RiN5IzvadIXTVefuuwZCRg== + version "4.5.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.3.tgz#afaa858e68c7103317d89eb90c5d8906268d353c" + integrity sha512-eVYaEHALSt+s9LbvgEv4Ef+Tdq7hBiIZgii12xXJnukryt3pMgJf6aKhoCZ3FWQsu6sydEnkg11fYXLzhLBjeQ== uglify-js@^3.1.4: version "3.7.6"