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 6 commits
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
23 changes: 2 additions & 21 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,25 +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

---

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

Find the overlapping variants between two string unions.
See: https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-377567046

---

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"author": "Tom Crockett",
"license": "MIT",
"devDependencies": {
"dtslint": "^0.2.0",
"typescript": "^2.6.2"
"dtslint": "^0.3.0",
"typescript": "^2.8.1"
},
"scripts": {
"test": "dtslint types"
Expand Down
6 changes: 0 additions & 6 deletions types/diff.ts

This file was deleted.

32 changes: 4 additions & 28 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
// TypeScript Version: 2.6

/**
* Remove the variants of the second union of string literals from
* the first.
*
* @see https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-307871458
*/
export type Diff<T extends string, U extends string> = (
& { [P in T]: P }
& { [P in U]: never }
& { [x: string]: never }
)[T];
// TypeScript Version: 2.8

/**
* Drop keys `K` from `T`.
*
* @see https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-307871458
*/
export type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>;

/**
* Find the overlapping variants between two string unions.
* @see https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-377567046
*/
export type Overlap<T extends string, U extends string> = Diff<T, Diff<T, U>>;
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

/**
* Like `T & U`, but where there are overlapping properties using the
Expand All @@ -31,7 +14,7 @@ export type Overlap<T extends string, U extends string> = Diff<T, Diff<T, U>>;
* @see https://github.com/pelotom/type-zoo/issues/2
* @see https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-307871458
*/
export type Overwrite<T, U> = Omit<T, Overlap<keyof T, keyof U>> & U;
export type Overwrite<T, U> = Omit<T, Extract<keyof T, keyof U>> & U;
Copy link
Owner

@pelotom pelotom Apr 23, 2018

Choose a reason for hiding this comment

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

Come to think of it, this could be (and always could have been) just

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

There was never any need for the old Overlap, or am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha, I don't know -- I didn't do TypeScript when that existed, does that pass the old tests? -- Do you want it changed to this?

I've never personally used the Overwrite operator, but your solution looks totally like an obvious implementation. I would probably have implemented it that way if I needed that operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, looking at my code, I have exactly that pattern, but I don't do the keyof T & keyof U and just target the Omit directly at the keys I want to junk & replace.

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, thinking it through I'm pretty sure Extract<keyof T, keyof U> is equivalent in all cases to keyof T & keyof U, so let's make that change.

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!


/**
* Use to prevent a usage of type `T` from being inferred in other generics.
Expand All @@ -48,13 +31,6 @@ 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
*/
export type NonNullable<T> = T & {};

/**
* Forgets contextual knowledge of partiality of keys.
*/
Expand Down
5 changes: 0 additions & 5 deletions types/non-nullable.ts

This file was deleted.

6 changes: 0 additions & 6 deletions types/overlap.ts

This file was deleted.

20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ [email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"

definitelytyped-header-parser@Microsoft/definitelytyped-header-parser#production:
"definitelytyped-header-parser@github:Microsoft/definitelytyped-header-parser#production":
version "0.0.0"
resolved "https://codeload.github.com/Microsoft/definitelytyped-header-parser/tar.gz/210b44cf9e7e5b9783a41df4f664695ade5325b1"
resolved "https://codeload.github.com/Microsoft/definitelytyped-header-parser/tar.gz/f074e863231ef0d79a31c0a9edaf1b82c98469ef"
dependencies:
"@types/parsimmon" "^1.3.0"
parsimmon "^1.2.0"
Expand All @@ -100,14 +100,14 @@ diff@^3.2.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.4.0.tgz#b1d85507daf3964828de54b37d0d73ba67dda56c"

dtslint@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/dtslint/-/dtslint-0.2.0.tgz#7723be0d974ad133ff1e5cdb84728669ca66c51e"
dtslint@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/dtslint/-/dtslint-0.3.0.tgz#918e664a6f7e1f54ba22088daa2bc6e64a6001c1"
dependencies:
definitelytyped-header-parser Microsoft/definitelytyped-header-parser#production
fs-promise "^2.0.0"
strip-json-comments "^2.0.1"
tslint "^5.4.2"
tslint "^5.9.1"
typescript next

escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
Expand Down Expand Up @@ -281,7 +281,7 @@ tslib@^1.8.0, tslib@^1.8.1:
version "1.9.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.0.tgz#e37a86fda8cbbaf23a057f473c9f4dc64e5fc2e8"

tslint@^5.4.2:
tslint@^5.9.1:
version "5.9.1"
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.9.1.tgz#1255f87a3ff57eb0b0e1f0e610a8b4748046c9ae"
dependencies:
Expand All @@ -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