mirror of
https://codeberg.org/teddit/teddit.git
synced 2026-04-23 22:35:05 -04:00
initial commit
This commit is contained in:
+39
@@ -0,0 +1,39 @@
|
||||
# Assert Never [![npm version][npm-image]][npm-url]
|
||||
|
||||
Helper function for [exhaustive checks][exhaustive-checks] of discriminated
|
||||
unions in TypeScript.
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
npm install --save assert-never
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```ts
|
||||
import {assertNever} from "assert-never";
|
||||
|
||||
type A = {type: 'a'};
|
||||
type B = {type: 'b'};
|
||||
type Union = A | B;
|
||||
|
||||
function doSomething(arg: Union) {
|
||||
if (arg.type === 'a') {
|
||||
return something;
|
||||
}
|
||||
|
||||
if (arg.type === 'b') {
|
||||
return somethingElse;
|
||||
}
|
||||
|
||||
// TS will error if there are other types in the union
|
||||
// Will throw an Error when called at runtime. Use `assertNever(arg, true)`
|
||||
// instead to fail silently.
|
||||
return assertNever(arg);
|
||||
}
|
||||
```
|
||||
|
||||
[npm-image]: https://badge.fury.io/js/assert-never.svg
|
||||
[npm-url]: https://badge.fury.io/js/assert-never
|
||||
[exhaustive-checks]: https://basarat.gitbooks.io/typescript/docs/types/discriminated-unions.html#exhaustive-checks
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Helper function for exhaustive checks of discriminated unions.
|
||||
* https://basarat.gitbooks.io/typescript/docs/types/discriminated-unions.html
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* type A = {type: 'a'};
|
||||
* type B = {type: 'b'};
|
||||
* type Union = A | B;
|
||||
*
|
||||
* function doSomething(arg: Union) {
|
||||
* if (arg.type === 'a') {
|
||||
* return something;
|
||||
* }
|
||||
*
|
||||
* if (arg.type === 'b') {
|
||||
* return somethingElse;
|
||||
* }
|
||||
*
|
||||
* // TS will error if there are other types in the union
|
||||
* // Will throw an Error when called at runtime.
|
||||
* // Use `assertNever(arg, true)` instead to fail silently.
|
||||
* return assertNever(arg);
|
||||
* }
|
||||
*/
|
||||
export declare function assertNever(value: never, noThrow?: boolean): never;
|
||||
export default assertNever;
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
/**
|
||||
* Helper function for exhaustive checks of discriminated unions.
|
||||
* https://basarat.gitbooks.io/typescript/docs/types/discriminated-unions.html
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* type A = {type: 'a'};
|
||||
* type B = {type: 'b'};
|
||||
* type Union = A | B;
|
||||
*
|
||||
* function doSomething(arg: Union) {
|
||||
* if (arg.type === 'a') {
|
||||
* return something;
|
||||
* }
|
||||
*
|
||||
* if (arg.type === 'b') {
|
||||
* return somethingElse;
|
||||
* }
|
||||
*
|
||||
* // TS will error if there are other types in the union
|
||||
* // Will throw an Error when called at runtime.
|
||||
* // Use `assertNever(arg, true)` instead to fail silently.
|
||||
* return assertNever(arg);
|
||||
* }
|
||||
*/
|
||||
function assertNever(value, noThrow) {
|
||||
if (noThrow) {
|
||||
return value;
|
||||
}
|
||||
throw new Error("Unhandled discriminated union member: " + JSON.stringify(value));
|
||||
}
|
||||
exports.assertNever = assertNever;
|
||||
exports.default = assertNever;
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Helper function for exhaustive checks of discriminated unions.
|
||||
* https://basarat.gitbooks.io/typescript/docs/types/discriminated-unions.html
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* type A = {type: 'a'};
|
||||
* type B = {type: 'b'};
|
||||
* type Union = A | B;
|
||||
*
|
||||
* function doSomething(arg: Union) {
|
||||
* if (arg.type === 'a') {
|
||||
* return something;
|
||||
* }
|
||||
*
|
||||
* if (arg.type === 'b') {
|
||||
* return somethingElse;
|
||||
* }
|
||||
*
|
||||
* // TS will error if there are other types in the union
|
||||
* // Will throw an Error when called at runtime.
|
||||
* // Use `assertNever(arg, true)` instead to fail silently.
|
||||
* return assertNever(arg);
|
||||
* }
|
||||
*/
|
||||
export function assertNever(value: never, noThrow?: boolean): never {
|
||||
if (noThrow) {
|
||||
return value
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Unhandled discriminated union member: ${JSON.stringify(value)}`,
|
||||
);
|
||||
}
|
||||
|
||||
export default assertNever;
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"_from": "assert-never@^1.2.1",
|
||||
"_id": "assert-never@1.2.1",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-TaTivMB6pYI1kXwrFlEhLeGfOqoDNdTxjCdwRfFFkEA30Eu+k48W34nlok2EYWJfFFzqaEmichdNM7th6M5HNw==",
|
||||
"_location": "/assert-never",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "assert-never@^1.2.1",
|
||||
"name": "assert-never",
|
||||
"escapedName": "assert-never",
|
||||
"rawSpec": "^1.2.1",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^1.2.1"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/with"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/assert-never/-/assert-never-1.2.1.tgz",
|
||||
"_shasum": "11f0e363bf146205fb08193b5c7b90f4d1cf44fe",
|
||||
"_spec": "assert-never@^1.2.1",
|
||||
"_where": "/home/user/site/node_modules/with",
|
||||
"author": {
|
||||
"name": "Daniel Lytkin",
|
||||
"email": "dan.lytkin@gmail.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/aikoven/assert-never/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"deprecated": false,
|
||||
"description": "Helper function for exhaustive checks of discriminated unions in TypeScript",
|
||||
"devDependencies": {
|
||||
"typescript": "^2.2.1"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.ts",
|
||||
"index.d.ts"
|
||||
],
|
||||
"homepage": "https://github.com/aikoven/assert-never#readme",
|
||||
"keywords": [
|
||||
"typescript",
|
||||
"discriminated unions",
|
||||
"assert",
|
||||
"never"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"name": "assert-never",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/aikoven/assert-never.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"prepublish": "npm run build"
|
||||
},
|
||||
"typings": "index.d.ts",
|
||||
"version": "1.2.1"
|
||||
}
|
||||
Reference in New Issue
Block a user