GregRos
GregRos

Reputation: 9103

Are TypeScript 2.0 definition files backwards compatible?

I want to write a TypeScript library. I want to use 2.0 because, you know, it's the newest and it has a pretty cool set of features. However, I'm worried the majority of people (who probably still use 1.8-) won't be able to use it.

Are TypeScript 2.0 d.ts definition files backwards compatible with version 1.8? If not, is there a set of best practices I can follow to ensure compatibility? Failing that, can I do anything else?

Upvotes: 1

Views: 497

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 220944

TypeScript 2.0 definition files are compatible with 1.8 as long as you don't expose any 2.0-specific features (such as readonly or numeric literal types).

The simplest thing to do would be to develop as normal, and use an npm-installed copy of the 1.8 compiler to validate that it's still able to parse the .d.ts file. Some other people in this situation have hooked in some Regexp replaces into their build chain to strip out a few 2.0-only features like readonly from their .d.ts files.

In the future there might be a tool that downlevels 2.0-specific features into their nearest 1.8-compatible equivalents, but no one has written it yet (FWIW it'd be relatively simple).


List of 2.0-specific features you might find in a .d.ts file

  • The never type
  • The explicit null and undefined types
  • Numeric and enum literal types (e.g. interface I { x: 0 })
  • this types in function expressions and function types (e.g. f(this: something))
  • UMD global definitions (export as namespace g)
  • readonly
  • Optional properties in classes
  • Private and protected constructors

Upvotes: 4

Related Questions