- Do be careful though – TypeScript will assume the package has the type any , meaning you can do anything with it.
- let a: Person[“age”]; This is the same as saying that n gets the type of the *name* property in Person.
- With mapped types, we no longer have to create new partial or readonly variants of existing types either.
- P gets bound to each of those literal types (even though it’s not used in this example), and gives the property the type boolean .
- Mapped types are produced by taking a union of literal types, and computing a set of properties for a new object type.
@basarat: #TypeScript 2.1
#ReactJS #AngularJS #Emberjs #Cyclejs #NodeJS
We spread ourselves thin, but this is the moment you’ve been awaiting – TypeScript 2.1 is here!
This release comes with features that we think will drastically reduce the friction of starting new projects, make the type-checker much more powerful, and give you the tools to write much more expressive code.
To start using TypeScript you can use NuGet, or install it through npm:
You can also grab the TypeScript 2.1 installer for Visual Studio 2015 after getting Update 3.
Visual Studio Code will usually just prompt you if your TypeScript install is more up-to-date, but you can also follow instructions to use TypeScript 2.1 now with Visual Studio Code or our Sublime Text Plugin.
and significantly improved inference, in our announcement for TypeScript 2.1 RC, but here’s a bit more about what’s new in 2.1.
and target ES3/ES5 without using any other tools.
to TypeScript involved rewriting our emit pipeline to use tree transforms. Keeping parity meant not just that existing emit didn’t change, but that TypeScript’s emit speed was on par as well. We’re pleased to say that after several months of testing, neither have been impacted, and that TypeScript users should continue to enjoy a stable speedy experience.
We’ve been excited to deliver object rest & spread since its original proposal, and today it’s here in TypeScript 2.1. Object rest & spread is a new proposal for ES2017 that makes it much easier to partially copy, merge, and pick apart objects. The feature is already used quite a bit when using libraries like Redux.
With object spreads, making a shallow copy of an object has never been easier:
We can even add new properties in the process:
would have been used instead of the one we explicitly wrote out.
Object rests are the dual of object spreads, in that they can extract any extra properties that don’t get picked up when destructuring an element:
Many libraries take advantage of the fact that objects are (for the most part) just a map of strings to values. Given what TypeScript knows about each value’s properties, there’s a set of known strings (or keys) that you can use for lookups.
operator comes in.
The above is equivalent to having written out
can be used as a query for types on values.
The dual of this is indexed access types, also called lookup types. Syntactically, they look exactly like an element access, but are written as types:
. In other words:
When indexing with a union of literal types, the operator will look up each property and union the respective types together.
This pattern can be used with other parts of the type system to get type-safe lookups, serving users of libraries like Ember.
Mapped types are definitely the most interesting feature in TypeScript 2.1.
, we might write the following:
Notice we had to define a completely new type.
Similarly, we might want to perform a shallow freeze of an object:
could have been shared.
with a mapped type.
Mapped types are produced by taking a union of literal types, and computing a set of properties for a new object type. They’re like list comprehensions in Python, but instead of producing new elements in a list, they produce new properties in a type.
operator to cut down on the typing:
And then we can generalize it:
With mapped types, we no longer have to create new partial or readonly variants of existing types either.
which could create an all-optional version of an existing type.
TypeScript has traditionally been a bit finnicky about exactly how you can import something. This was to avoid typos and prevent users from using packages incorrectly.
However, a lot of the time, you might just want to write a quick script and get TypeScript’s editing experience. Unfortunately, it’s pretty common that as soon as you import something you’ll get an error.
“But I already have that package installed!” you might say.
The problem is that TypeScript didn’t trust the import since it couldn’t find any declaration files for lodash. The fix is pretty simple:
But this was a consistent point of friction for developers. And while you can still compile & run your code in spite of those errors, those red squiggles can be distracting while you edit.
So we focused on on that one core expectation:
and from that statement, the solution became obvious. We decided that TypeScript needs to be more trusting, and in TypeScript 2.1, so long as you have a package installed, you can use it.
, which we actually recommend for all new TypeScript projects.
options, better support for custom elements, and support for importing helper functions, all which you can read about on our wiki.
As always, we’d love to hear your feedback, so give 2.1 a try and let us know how you like it! Happy hacking!
Our app has a C# object model where every object is a dictionary and the objects in the dictionary are also dictionary. It’s easy to add/remove properties from any object.