Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Conversation

@mhegazy
Copy link
Contributor

@mhegazy mhegazy commented Nov 24, 2016

//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.


```ts
// From T pick a set of properties K
function pick<T, K extends keyof T>(obj: T, ...keys: K[]): Pick<T, K>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declare function

Copy link
Contributor Author

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`.
Copy link
Member

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

Copy link
Member

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.

Copy link
Contributor Author

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.
Copy link
Member

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

Copy link
Contributor Author

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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the tslib utility library

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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

Copy link
Contributor Author

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()`.
Copy link
Member

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

Copy link
Contributor Author

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.
Copy link
Member

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...

Copy link
Contributor Author

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.
Copy link
Member

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

Copy link
Contributor Author

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`
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Member

@sandersn sandersn left a 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
Copy link
Member

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 ...

Copy link
Contributor Author

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*.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete 'actually'

Copy link
Contributor Author

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.
Copy link
Member

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."

Copy link
Member

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 ..."

Copy link
Contributor Author

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.
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

computing

Copy link
Contributor Author

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: available → variable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Member

@sandersn sandersn left a 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.
Copy link
Member

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.
Copy link
Member

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.
Copy link
Member

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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a not n

Copy link
Contributor Author

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.
Copy link
Member

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For → because of

Copy link
Contributor Author

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> = {
Copy link
Member

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

Copy link
Contributor Author

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo:concrete

Copy link
Contributor Author

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`.
Copy link
Member

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, ..."

Copy link
Contributor Author

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decides → determines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@mhegazy
Copy link
Contributor Author

mhegazy commented Dec 1, 2016

@DanielRosenwasser and @sandersn, I have simplified the notes for keyof and mapped type, less chatty now.

Copy link
Member

@sandersn sandersn left a 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`;
Copy link
Member

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:
Copy link
Member

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. "enable expressing" → express
  2. typo: useful
  3. 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:
Copy link
Member

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

version

@mhegazy mhegazy merged commit 3b7f2f9 into master Dec 5, 2016
@mhegazy mhegazy deleted the 2.1ReleaseNotes branch December 5, 2016 21:37
@mhegazy
Copy link
Contributor Author

mhegazy commented Dec 5, 2016

@DanielRosenwasser let me know if you have more comments and i will get them done in a separate PR.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants