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

negated types #140

Merged
merged 5 commits into from
Oct 5, 2018
Merged
Changes from 4 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
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Intuitive structural type notation for JavaScript.
- [Literal Types](#literal-types)
- [Tuples](#tuples)
- [Union Types](#union-types)
- [Negated Types](#negated-types)
- [Constructors](#constructors)
- [Accessor Descriptors](#accessor-descriptors)
- [Throwing Functions](#throwing-functions)
Expand Down Expand Up @@ -199,7 +200,7 @@ Array, Boolean, Function, Number, Object, RegExp, String, Symbol
ArrayBuffer, Date, Error, Map, Promise, Proxy, Set, WeakMap, WeakSet
```

Note: `null` is part of `Any` and is *not* covered by `Object`.
Note: `null` is part of `Any` and is *not* covered by `Object`. If you want to allow `null` with `Object`, you must specify the union explicitly: `Object | null`

#### The `Any` Type

Expand Down Expand Up @@ -242,17 +243,17 @@ Arrays, typed arrays, strings, maps and sets are iterable. Additionally any obje
Is equivalent to

```ts
interface IterableObject {
[Symbol.iterator]: () => Iterator
}

interface Iterator {
next() => {
done: Boolean,
value?: Any
}
}

interface IterableObject {
[Symbol.iterator]: () => Iterator
}

(paramName: IterableObject) => Void
```

Expand Down Expand Up @@ -294,7 +295,29 @@ For **∅ or more** and **1 or more** element(s) of the same type you can use th
Union types are denoted with the pipe symbol, `|`:

```js
(userInput: String|Number) => String|Number
(userInput: String | Number) => String | Number
```

### Negated Types

It is sometime easier and more informative to delimit a type by defining what it's not. The negation operator let's you exclude by substracting from `Any`.
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/lets/


```js
JSON::parse(String, reviver: Function)
=> Boolean | Number | String | Object | Array | null,
Copy link
Collaborator Author

@Mouvedia Mouvedia Dec 29, 2018

Choose a reason for hiding this comment

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

@ericelliott there's a problem with this example: Array and Object are not precise enough. These objects and arrays must only contain JSON values; i.e. it's recursive.

The example needs to be updated.

throws SyntaxError

// is less concise than

JSON::parse(String, reviver: Function)
=> !Function & !Void & !Symbol,
Mouvedia marked this conversation as resolved.
Show resolved Hide resolved
throws SyntaxError

// which is equivalent to
Mouvedia marked this conversation as resolved.
Show resolved Hide resolved

JSON::parse(String, reviver: Function)
=> !(Function & Void & Symbol),
throws SyntaxError
```
Mouvedia marked this conversation as resolved.
Show resolved Hide resolved

### Constructors
Expand Down