Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript 2.8 — initial support & updates #14

Merged
merged 7 commits into from
Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@ yarn add type-zoo

## API

### `Diff<T extends string, U extends string>`

Remove the variants of the second union of string literals from the first.

See: https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-307871458

---

### `NoInfer<T>`

Use to prevent a usage of type `T` from being inferred in other generics.

Example:

```ts
declare function assertEqual<T>(actual: T, expected: NoInfer<T>): boolean;
```
Expand All @@ -38,19 +33,11 @@ See: https://github.com/Microsoft/TypeScript/issues/14829#issuecomment-322267089

---

### `NonNullable<T>`

`T` without the possibility of `undefined` or `null`.

See: https://github.com/Microsoft/TypeScript/issues/15012#issuecomment-346499713

---

### `Omit<T, K extends keyof T>`

Drop keys `K` from `T`.

See: https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-307871458
See: https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-377567046

---

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"license": "MIT",
"devDependencies": {
"dtslint": "^0.2.0",
"typescript": "^2.6.2"
"typescript": "^2.8.1"
},
"scripts": {
"test": "dtslint types"
Expand Down
21 changes: 12 additions & 9 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// TypeScript Version: 2.6
// TypeScript Version: 2.8

/**
* Remove the variants of the second union of string literals from
* the first.
*
* @see https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-307871458
* TypeScript Documentation suggests that as of 2.8 you should use the built-in "Exclude" instead.
*
* @see https://github.com/Microsoft/TypeScript-Handbook/blame/master/pages/release%20notes/TypeScript%202.8.md#L245
Copy link
Owner

Choose a reason for hiding this comment

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

Let's either alias Diff to Exclude or just take it out; I think I'd be fine with the latter and bumping the major version since we're already making the breaking change of requiring TypeScript 2.8.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently making it an Alias, but happy to delete it instead, please advise!

Copy link
Owner

Choose a reason for hiding this comment

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

Let's delete.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Deleted!

*/
export type Diff<T extends string, U extends string> = (
& { [P in T]: P }
Expand All @@ -15,9 +17,9 @@ export type Diff<T extends string, U extends string> = (
/**
* Drop keys `K` from `T`.
*
* @see https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-307871458
* @see https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-377567046
*/
export type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>;
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

/**
* Find the overlapping variants between two string unions.
Copy link
Owner

Choose a reason for hiding this comment

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

Can we replace Overlap with Extract?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not familiar with Overlap or the internals of Extract yet, happy to do whatever you suggest!

Copy link
Owner

Choose a reason for hiding this comment

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

/**
 * Extract from T those types that are assignable to U
 */
type Extract<T, U> = T extends U ? T : never;

Diff and Overlap are dual, as Exclude and Extract are dual.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool! So what's the specific action you would like here? Sounds like the answer to your first question is "Yes", but do you want me to just delete the Overlap implementation from type-zoo?

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, I think Diff and Overlap have been superseded by Exclude and Extract respectively.

Copy link
Owner

Choose a reason for hiding this comment

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

So let's delete both Diff and Overlap.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Deleted!

Expand All @@ -29,9 +31,11 @@ export type Overlap<T extends string, U extends string> = Diff<T, Diff<T, U>>;
* type from U only.
*
* @see https://github.com/pelotom/type-zoo/issues/2
* @see https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-307871458
* @see https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-377692897
*/
export type Overwrite<T, U> = Omit<T, Overlap<keyof T, keyof U>> & U;
export type Overwrite<T, U> = {
Copy link
Owner

Choose a reason for hiding this comment

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

I don't see why this one needs to change except to use Extract instead of Overlap:

export type Overwrite<T, U> = Omit<T, Extract<keyof T, keyof U>> & U;

[P in Exclude<keyof T, keyof U>]: T[P]
} & U;

/**
* Use to prevent a usage of type `T` from being inferred in other generics.
Expand All @@ -49,9 +53,8 @@ export type Overwrite<T, U> = Omit<T, Overlap<keyof T, keyof U>> & U;
export type NoInfer<T> = T & { [K in keyof T]: T[K] };

/**
* `T` without the possibility of `undefined` or `null`.
*
* @see https://github.com/Microsoft/TypeScript/issues/15012#issuecomment-346499713
* Deprecated: available in TypeScript Core as of 2.8
* @see https://github.com/Microsoft/TypeScript-Handbook/blame/master/pages/release%20notes/TypeScript%202.8.md#L203
Copy link
Owner

Choose a reason for hiding this comment

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

Let's just take it out.

*/
export type NonNullable<T> = T & {};

Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,9 @@ tsutils@^2.12.1:
dependencies:
tslib "^1.8.1"

typescript@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.6.2.tgz#3c5b6fd7f6de0914269027f03c0946758f7673a4"
typescript@^2.8.1:
version "2.8.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.8.1.tgz#6160e4f8f195d5ba81d4876f9c0cc1fbc0820624"

typescript@next:
version "2.8.0-dev.20180127"
Expand Down