-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add TypeScript 2.1 release notes #440
Conversation
|
|
||
| ```ts | ||
| // From T pick a set of properties K | ||
| function pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
declare function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
| # Support for external helpers library (`tslib`) | ||
|
|
||
| TypeScript injects a handful of helper functions such as `__extends` for inheritance, `__assign` for spread operator in JSX, and `__awaiter` for async functions. | ||
| Previously there were two options either 1. inject helpers in *every* file that needs them or 2. no helpers at all with `--noEmitHelpers`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turn these into an ordered list
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mention that this was a painpoint if users wanted to keep their bundle size low.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done and done.
|
|
||
| # Support for external helpers library (`tslib`) | ||
|
|
||
| TypeScript injects a handful of helper functions such as `__extends` for inheritance, `__assign` for spread operator in JSX, and `__awaiter` for async functions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assign is also used beyond JSX
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
|
|
||
| TypeScript 2.1 allows for including these files in your project once in a separate module, and the compiler will emit imports to them as needed. | ||
|
|
||
| First, install the [`tslib`](https://github.com/Microsoft/tslib): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the tslib utility library
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
| tsc --module commonjs --importHelpers a.ts | ||
| ``` | ||
|
|
||
| ##### Example |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need this heading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
|
|
||
| ```ts | ||
|
|
||
| const c1 = 1; // Type 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surround each type with a single quote
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that is confusing.. it is not type '1' it is type 1
|
|
||
| # Use returned values from super calls as 'this' | ||
|
|
||
| In ES2015, constructors which return a value (which is an object) implicitly substitute the value of `this` for any callers of `super()`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"constructors which return an object" might be shorter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
| TypeScript 2.1 supports inheriting configuration using `extends`, where: | ||
|
|
||
| * `extends` is a new top-level property in `tsconfig.json` (alongside `compilerOptions`, `files`, `include`, and `exclude`). | ||
| * `extends`' value is a string containing a path to another configuration file to inherit from. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value of extends must be a string...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
| * `extends` is a new top-level property in `tsconfig.json` (alongside `compilerOptions`, `files`, `include`, and `exclude`). | ||
| * `extends`' value is a string containing a path to another configuration file to inherit from. | ||
| * The configuration from the base file are loaded first, then overridden by those in the inheriting config file. | ||
| * If a circularity is encountered, we report an error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
circularity between configuration files is encountered
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to Circularity between configuration files is not allowed.
| Modules are parsed automatically in strict mode. | ||
| The new flag is recommended for non-module code. | ||
|
|
||
| # Support for `--target ES2016`, `--target ES2017` and `--target ESNext` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this higher up - probably before implicit any inference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
sandersn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are my comments for now. I need to compare the presentation of mapped types with the one in the Handbook PR I made. I'll comment more at that time.
| let propName: keyof Person; | ||
| ``` | ||
|
|
||
| The above is equivalent to having written out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keyof Person is equivalent to ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
| let propName: "name" | "age" | "location"; | ||
| ``` | ||
|
|
||
| This `keyof` operator is actually called an *index type query*. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete 'actually'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
| let nameOrAge: Person["name" | "age"]; | ||
| ``` | ||
|
|
||
| This pattern can be used with other parts of the type system to get type-safe lookups, serving users of libraries like Ember. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The last part is pretty formal. I'd rewrite to: ", which is great if you use Ember or something like it."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, might as well reword the beginning too: "You can use this pattern with ..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
| } | ||
| ``` | ||
|
|
||
| Or we might want to create a related type where all the properties are `Promise`s. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example is wrong. It should be Promise, or else the sentence should talk about booleans.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably the sentence should talk about booleans, given the extended example below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
| }; | ||
| ``` | ||
|
|
||
| Mapped types are produced by taking a union of literal types, and compute a set of properties for a new object type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
computing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
| ## Implicit any errors | ||
|
|
||
| One great benefit of this is that you'll see *way fewer* implicit `any` errors when running with `--noImplicitAny`. | ||
| Implicit `any` errors are only reported when the compiler is unable to know the type of a available without a type annotation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: available → variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
sandersn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some more comments, on mapped types mostly.
| } | ||
| ``` | ||
|
|
||
| Much of the time, we want to take an existing type and make each of its properties entirely optional. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rewrite as "One common task is to take an existing type ..."
| } | ||
| ``` | ||
|
|
||
| Or we might want to create a related type where all the properties are `Promise`s. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably the sentence should talk about booleans, given the extended example below.
| # `keyof` and Lookup Types | ||
|
|
||
| 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is feature-oriented, which is fine for a release notes format. In the handbook, I put a cool example of usage up front and afterward explain how it works. I think that's better for blog and instructional formats.
| let a: Person["age"]; | ||
| ``` | ||
|
|
||
| This is the same as saying that `n` gets the type of the `name` property in `Person`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a not n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
| let nameOrAge: Person["name" | "age"]; | ||
| ``` | ||
|
|
||
| This pattern can be used with other parts of the type system to get type-safe lookups, serving users of libraries like Ember. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, might as well reword the beginning too: "You can use this pattern with ..."
| function freeze<T>(obj: T): Readonly<T>; | ||
| ``` | ||
|
|
||
| For that, they are now included by default in the standard library. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For → because of
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
| }; | ||
|
|
||
| // Same property names, but make the value a promise instead of a concreate one | ||
| type Defered<T> = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't Deferred the correct spelling? I'm not sure anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
| readonly [P in keyof T]: T[P]; | ||
| }; | ||
|
|
||
| // Same property names, but make the value a promise instead of a concreate one |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo:concrete
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
| let copy = { ...original }; | ||
| ``` | ||
|
|
||
| Similarly, you can merge several different objects so that in the following example, `merged` will have properties from `foo`, `bar`, and `baz`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would break this into two sentences: "Similarly, you can merge several different objects. In the following example, ..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
| var newObj = {...obj, z: 3, y: 4}; // { x: number, y:number, x: number } | ||
| ``` | ||
|
|
||
| The order of specifying spread operations decides what properties end up in the resulting object; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
decides → determines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
|
@DanielRosenwasser and @sandersn, I have simplified the notes for |
sandersn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few small typos left to fix, otherwise looks good.
| In JavaScript it is fairly common to have APIs that expect property names as parameters, but so far it hasn't been possible to express the type relationships that occur in those APIs. | ||
|
|
||
| That's where the `keyof` operator comes in. | ||
| Enter Index Type Query or `keysof`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: keyof
|
|
||
| Much of the time, we want to take an existing type and make each of its properties entirely optional. | ||
| With `Person`, we might write the following: | ||
| A partial verion of it can would be: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete 'can'
| They're like [list comprehensions in Python](https://docs.python.org/2/tutorial/datastructures.html#nested-list-comprehensions), but instead of producing new elements in a list, they produce new properties in a type. | ||
|
|
||
| With mapped types, we no longer have to create new partial or readonly variants of existing types either. | ||
| In addition to `Partial`, Mapped Types can enable expressing many usueful transfomrations on types: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- "enable expressing" → express
- typo: useful
- typo: transformations
| Previously there were two options either 1. inject helpers in *every* file that needs them or 2. no helpers at all with `--noEmitHelpers`. | ||
| TypeScript injects a handful of helper functions such as `__extends` for inheritance, `__assign` for spread operator in object literals and JSX elements, and `__awaiter` for async functions. | ||
|
|
||
| Previously there were two options either: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just delete 'either'. Or say "Previously there were two options; either:"
| } | ||
| ``` | ||
|
|
||
| A partial verion of it can would be: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
version
|
@DanielRosenwasser let me know if you have more comments and i will get them done in a separate PR. |
//CC @DanielRosenwasser, @sandersn
Some parts copied verbatim from @DanielRosenwasser's blog.
This includes the content already in https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript.