├── README.md
├── StrictArg.code-workspace
├── index.d.ts
├── package.json
└── tsconfig.json
/README.md:
--------------------------------------------------------------------------------
1 | # ts-strictargs
2 | Provides types to specify how arguments are checked
3 |
4 | # Included
5 | ```Typescript
6 | /** Checks B is a subset of A and returns B */
7 | type Subset
8 |
9 | /** Checks B is a superset of A and returns B */
10 | type Superset
11 |
12 | /** Checks B is exactly A and returns B */
13 | type Strict
14 |
15 | /** Checks and returns the union of A and B */
16 | type Loose
17 | ```
18 | # Usage
19 | * ```|b?: XXX``` indicates where behaviour would differ if BaseOptions.b was optional
20 | ```Typescript
21 | type BaseOptions = { a: string, b: number }
22 |
23 | const options0 = { a: "hi", b: 4 }
24 | const options1 = { a: 5, b: 4 }
25 | const options2 = { a: "o", b: "hello" }
26 | const options3 = { a: "o" }
27 | const options4 = { b: 4 }
28 | const options5 = { a: "o", b: 4, c: 5 }
29 |
30 | const noMore = >(options: T) => { }
31 | noMore(options0) //Fine
32 | noMore(options1) //Error
33 | noMore(options2) //Error
34 | noMore(options3) //Fine
35 | noMore(options4) //Fine
36 | noMore(options5) //Error
37 |
38 | // Checks there are not less properties (More fine, Not Less)
39 | const noLess = >(options: T) => { }
40 | noLess(options0) //Fine
41 | noLess(options1) //Error
42 | noLess(options2) //Error
43 | noLess(options3) //Error |b?: Fine
44 | noLess(options4) //Error
45 | noLess(options5) //Fine
46 |
47 |
48 | const strict = >(options: T) => { }
49 | strict(options0) //Fine
50 | strict(options1) //Error
51 | strict(options2) //Error
52 | strict(options3) //Error |b?: Fine
53 | strict(options4) //Error
54 | strict(options5) //Error
55 |
56 | const loose = >(options: T) => { }
57 | loose(options0) //Fine
58 | loose(options1) //Error
59 | loose(options2) //Error
60 | loose(options3) //Fine
61 | loose(options4) //Fine
62 | loose(options5) //Fine
63 | ```
64 | # Install
65 | [https://www.npmjs.com/package/ts-strictargs](https://www.npmjs.com/package/ts-strictargs)
66 | ```
67 | $ npm install --save-dev ts-strictargs
68 | ```
69 |
70 | # Github
71 | [https://github.com/Kotarski/ts-strictargs](https://github.com/Kotarski/ts-strictargs)
72 |
--------------------------------------------------------------------------------
/StrictArg.code-workspace:
--------------------------------------------------------------------------------
1 | {
2 | "folders": [
3 | {
4 | "path": "."
5 | }
6 | ]
7 | }
--------------------------------------------------------------------------------
/index.d.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Checks B is a subset of A and returns B
3 | * @example const noMore = >(options: T) => { }
4 | */
5 | export type Subset = {
6 | [P in keyof B]: P extends keyof A ? (B[P] extends A[P] | undefined ? A[P] : never) : never;
7 | }
8 |
9 | /**
10 | * Checks B is a superset of A and returns B
11 | * @example const noLess = >(options: T) => { }
12 | */
13 | export type Superset = Subset;
14 |
15 | /**
16 | * Checks B is exactly A and returns B
17 | * @example const strict = >(options: T) => { }
18 | */
19 | export type Strict = Subset & Superset;
20 |
21 | /**
22 | * Checks and returns the union of A and B
23 | * @example const loose = >(options: T) => { }
24 | */
25 | export type Loose = Subset | Superset;
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ts-strictargs",
3 | "version": "1.0.2",
4 | "description": "Provides types to specify how arguments are checked",
5 | "types": "index.d.ts",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "git+https://github.com/Kotarski/ts-strictargs.git"
12 | },
13 | "keywords": [
14 | "Typescript"
15 | ],
16 | "author": "Edward Kotarski",
17 | "license": "ISC",
18 | "bugs": {
19 | "url": "https://github.com/Kotarski/ts-strictargs/issues"
20 | },
21 | "homepage": "https://github.com/Kotarski/ts-strictargs#readme",
22 | "devDependencies": {
23 | "typescript": "^3.1.0"
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "es5",
4 | "module": "commonjs",
5 | "strict": true
6 | }
7 | }
--------------------------------------------------------------------------------