简体   繁体   English

如何使 4.X Typescript 项目与旧版本的 Typescript 3.X 兼容?

[英]How to make 4.X Typescript project compatible with older version of Typescript 3.X?

How to make package built on top of TS 4.X compatible with 3.X?如何使建立在 TS 4.X 之上的 package 与 3.X 兼容? Like, if you have newer version -> use new features, else -> use any or unknown or whatever is supported in older version.就像,如果您有更新的版本 -> 使用新功能,否则 -> 使用anyunknown或旧版本支持的任何内容。

Is there any possibility to use directives for that purpose?有没有可能为此目的使用指令

You can just use an install an older version (whatever you want it to be, example: `npm i --dev typescript@3.5.2) of typescript in your npm and try to fix the errors.您可以在 npm 中使用安装 typescript 的旧版本(无论您想要什么,例如:`npm i --dev typescript@3.5.2)并尝试修复错误。 Then revert the typescript version to v4 or whatever it was already.然后将 typescript 版本恢复到 v4 或其他任何版本。

Now your code is compatible with the designated older typescript version (just make sure the functionality of your code doesn't change while updating it).现在您的代码与指定的旧 typescript 版本兼容(只需确保代码的功能在更新时不会改变)。

There is no way to switch between supported and unsupported features in TypeScript.在 TypeScript 中,无法在支持和不支持的功能之间切换。

For instance, if you have a library which is built on top of TS 4.0 with variadic tuple types there is no way to use it in a package where TS 3.0 is used.例如,如果您有一个构建在TS 4.0之上且具有variadic tuple types的库,则无法在使用 TS 3.0 的 package 中使用它。

However, you can maintain two versions of your typings: before TS4 and after TS4.但是,您可以维护两个版本的分型:TS4 之前和 TS4 之后。 For instance, take a look how lodash or react maintaince several versions of typings.例如,看看lodashreact如何维护几个版本的类型。

Note: TypeScript has a feature to "downlevel" the JavaScript it emits to convert language constructs from later versions of the ECMA Script standard to constructs that work in older versions.注意:TypeScript 具有“降级”它发出的 JavaScript 的功能,用于将来自更高版本的 ECMA 脚本标准的语言构造转换为在旧版本中工作的构造。 (see the compileOptions.target field of tsconfig ). (参见tsconfig 的compileOptions.target字段)。

As far as I know, TypeScript itself doesn't have such a feature to downlevel the typings files it emits (including directives at the time of this writing), but Nathan Sanders (a maintainer of Definitely Typed) maintains an open-source project, downlevel-dts , to downlevel.d.ts files which can downlevel typings all the way down to typescript v3.4 syntax.据我所知, TypeScript本身没有这样的功能来降低它发出的类型文件的级别(包括撰写本文时的指令),但是 Nathan Sanders(Definite Typed 的维护者)维护了一个开源项目, downlevel-dts ,到 downlevel.d.ts 文件,这些文件可以将类型一直降低到 typescript v3.4 语法。

You will need to hook it into your build process somehow.您需要以某种方式将其挂接到您的构建过程中。

Here's what I think could be problematic with other proposed solutions:以下是我认为其他提议的解决方案可能存在的问题:

  • "Use an older typescript version to emit your typings" “使用较旧的 typescript 版本来发出您的打字”

    • This assumes the asker of the question is using the TS compiler to emit typings from.ts files and doesn't work if they are maintaining them by hand, which is often the case for very large projects that were first written in JS and don't have the bandwidth to migrate to TS.这假设问题的提出者正在使用 TS 编译器从 .ts 文件中发出类型,并且如果他们手动维护它们则不起作用,这对于最初用 JS 编写的非常大的项目通常是这种情况并且不要'没有带宽迁移到 TS。
    • This probably requires you to make your.ts source code to not use TypeScript language constructs that were introduced in a version newer than the compiler that you have to use to emit typings in the TypeScript language version you want to emit.这可能要求您使 your.ts源代码不使用TypeScript 语言结构,这些语言结构是在比编译器更新的版本中引入的,您必须使用该编译器在您想要发出的 TypeScript 语言版本中发出类型。 This is not ideal because it may be much cleaner to write code using newer language constructs, or impossible to do something with just older constructs.这并不理想,因为使用较新的语言结构编写代码可能会更简洁,或者仅使用较旧的结构来做某事是不可能的。
  • "Maintain typings for both TypeScript language version" “维护 TypeScript 语言版本的类型”

    • This seems to make the opposite assumption: That the project maintains manual typings files for manually written JS code instead of being transpiled to.js and emitting.d.ts from.ts files.这似乎做出了相反的假设:该项目为手动编写的 JS 代码维护手动键入文件,而不是被转换为.js 并从.ts 文件中发出.d.ts。
    • This is a big maintenance burden (it's already a big maintenance burden to manually maintain.d.ts files.), It may be acceptable for small projects.这是一个很大的维护负担(手动维护.d.ts文件已经是一个很大的维护负担。),对于小型项目来说可能是可以接受的。 but not for libraries with a large or complicated API surface.但不适用于具有大型或复杂 API 表面的库。

For any bored/keen readers who want to learn more about this general topic and good practices, semantic versioning of typescript typings is quite thoroughly discussed on semver-ts.org , which isn't a short read, but a good one.对于任何想要了解更多关于这个一般主题和良好实践的无聊/热心读者,typescript 类型的语义版本控制在semver-ts.org上进行了非常彻底的讨论,这不是一篇简短的文章,而是一篇很好的文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM