Comparing version 1.1.1 to 1.1.2
declare const corrupt: (unreachable: never) => never; | ||
type AnyFunction = (...args: any[]) => unknown; | ||
type ExhaustiveUnion<Union extends string | boolean> = { | ||
[Key in `${Union}`]: (value: Key extends 'true' ? true : Key extends 'false' ? false : Key) => any; | ||
} & ExhaustiveFallback; | ||
type ExhaustiveTag<Union extends object, Tag extends keyof Union> = Union[Tag] extends string | boolean ? { | ||
type NoInfer<T> = [T][T extends any ? 0 : never]; | ||
/** | ||
* `unknown` extends `any` will be true, | ||
* whereas `unknown` extends other types will be false | ||
*/ | ||
type ValidateOutput<T> = unknown extends T ? unknown : T; | ||
type ExhaustiveUnion<Union extends string | boolean, Output = unknown> = [ | ||
Union | ||
] extends [string] ? { | ||
[Key in Union]: (value: Key) => Output; | ||
} & ExhaustiveDefaultCase<NoInfer<Output>> : Union extends boolean ? { | ||
true: (value: true) => Output; | ||
false: (value: false) => Output; | ||
} & ExhaustiveDefaultCase<NoInfer<Output>> : never; | ||
type ExhaustiveTag<Union extends object, Tag extends keyof Union, Output = unknown> = [ | ||
Union[Tag] | ||
] extends [string] ? { | ||
[Key in `${Union[Tag]}`]: (value: Extract<Union, { | ||
[K in Tag]: Key extends 'true' ? true : Key extends 'false' ? false : Key; | ||
}>) => any; | ||
} & ExhaustiveFallback : never; | ||
type ExhaustiveFallback = { | ||
[K in Tag]: Key; | ||
}>) => Output; | ||
} & ExhaustiveDefaultCase<NoInfer<Output>> : Union[Tag] extends boolean ? { | ||
true: (value: Extract<Union, { | ||
[K in Tag]: true; | ||
}>) => Output; | ||
false: (value: Extract<Union, { | ||
[K in Tag]: false; | ||
}>) => Output; | ||
} & ExhaustiveDefaultCase<NoInfer<Output>> : never; | ||
type ExhaustiveDefaultCase<Output> = { | ||
/** | ||
* Default fallback | ||
* Default case | ||
* | ||
* @description | ||
* When declared, "exhaustive" will fallback to this callback | ||
* instead of throwing an unreachable error | ||
* When declared, "exhaustive" will fallback to this case | ||
* instead of throwing an unreachable error on unmatched case | ||
*/ | ||
_?: () => any; | ||
_?: (value: never) => Output; | ||
}; | ||
@@ -25,11 +44,15 @@ /** | ||
*/ | ||
type ValidateKeys<T, U> = [keyof T] extends [keyof U] ? T : { | ||
type ValidateKeys<T, U> = [ | ||
keyof T | ||
] extends [keyof U] ? T : { | ||
[Key in keyof U]: Key extends keyof T ? T[Key] : never; | ||
}; | ||
declare function exhaustive<Union extends string | boolean, Match extends ExhaustiveUnion<Union> = ExhaustiveUnion<Union>, Output = Match[keyof Match] extends AnyFunction ? ReturnType<Match[keyof Match]> : never>(union: Union, match: ValidateKeys<Match, ExhaustiveUnion<Union>>): Output; | ||
declare function exhaustive<Union extends object, Tag extends keyof Union, Match extends ExhaustiveTag<Union, Tag> = ExhaustiveTag<Union, Tag>, Output = Match[keyof Match] extends AnyFunction ? ReturnType<Match[keyof Match]> : never>(union: Union, tag: Tag, match: ValidateKeys<Match, ExhaustiveTag<Union, Tag>>): Output; | ||
type MatchCases<InferredCases, StrictCases, Output> = unknown extends Output ? InferredCases : StrictCases; | ||
type ExtractOutput<Cases extends Record<string, (...args: any) => unknown>, Output> = unknown extends Output ? ValidateOutput<ReturnType<Cases[keyof Cases]>> : Output; | ||
declare function exhaustive<Union extends string | boolean, Output, Cases extends ExhaustiveUnion<Union> = ExhaustiveUnion<Union>>(union: Union, match: MatchCases<ValidateKeys<Cases, ExhaustiveUnion<Union>>, ExhaustiveUnion<Union, Output>, Output>): ExtractOutput<Cases, Output>; | ||
declare function exhaustive<Union extends object, Tag extends keyof Union, Output, Cases extends ExhaustiveTag<Union, Tag> = ExhaustiveTag<Union, Tag>>(union: Union, tag: Tag, match: MatchCases<ValidateKeys<Cases, ExhaustiveTag<Union, Tag>>, ExhaustiveTag<Union, Tag, Output>, Output>): ExtractOutput<Cases, Output>; | ||
declare namespace exhaustive { | ||
var tag: <Union extends object, Tag extends keyof Union, Match extends ExhaustiveTag<Union, Tag> = ExhaustiveTag<Union, Tag>, Output = Match[keyof Match] extends AnyFunction ? ReturnType<Match[keyof Match]> : never>(union: Union, tag: Tag, match: ValidateKeys<Match, ExhaustiveTag<Union, Tag>>) => Output; | ||
var tag: <Union extends object, Tag extends keyof Union, Output, Cases extends ExhaustiveTag<Union, Tag, unknown> = ExhaustiveTag<Union, Tag, unknown>>(union: Union, tag: Tag, cases: MatchCases<ValidateKeys<Cases, ExhaustiveTag<Union, Tag, unknown>>, ExhaustiveTag<Union, Tag, Output>, Output>) => ExtractOutput<Cases, Output>; | ||
} | ||
export { ExhaustiveTag, ExhaustiveUnion, corrupt, exhaustive }; | ||
export { type ExhaustiveTag, type ExhaustiveUnion, corrupt, exhaustive }; |
@@ -1,1 +0,1 @@ | ||
"use strict";var r=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var h=(t,n)=>{for(var e in n)r(t,e,{get:n[e],enumerable:!0})},g=(t,n,e,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let a of x(n))!p.call(t,a)&&a!==e&&r(t,a,{get:()=>n[a],enumerable:!(o=f(n,a))||o.enumerable});return t};var d=t=>g(r({},"__esModule",{value:!0}),t);var l={};h(l,{corrupt:()=>i,exhaustive:()=>s});module.exports=d(l);var i=t=>{let n=e=>{if(typeof e=="symbol")return e.toString();if(typeof e=="undefined")return"undefined";if(typeof e=="string")return e;try{return JSON.stringify(e)}catch(o){if(o instanceof TypeError)return typeof e=="bigint"?`${e.toString()} (bigint)`:"circular object";throw o}};throw new TypeError(`Internal Error: encountered impossible value "${n(t)}"`)};function s(t,n,e){if(typeof e!="undefined"){let y=t,c=n;return s.tag(y,c,e)}let o=t,a=n;if(!Object.prototype.hasOwnProperty.call(a,o))return Object.prototype.hasOwnProperty.call(a,"_")?a._():i(o);let u=a[o];return u(o)}s.tag=(t,n,e)=>{let o=t[n];if(!Object.prototype.hasOwnProperty.call(e,o))return Object.prototype.hasOwnProperty.call(e,"_")?e._():i(t);let a=e[o];return a(t)};0&&(module.exports={corrupt,exhaustive}); | ||
"use strict";var i=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var T=(e,n)=>{for(var t in n)i(e,t,{get:n[t],enumerable:!0})},h=(e,n,t,a)=>{if(n&&typeof n=="object"||typeof n=="function")for(let u of f(n))!g.call(e,u)&&u!==t&&i(e,u,{get:()=>n[u],enumerable:!(a=y(n,u))||a.enumerable});return e};var O=e=>h(i({},"__esModule",{value:!0}),e);var d={};T(d,{corrupt:()=>o,exhaustive:()=>r});module.exports=O(d);var o=e=>{let n=t=>{if(typeof t=="symbol")return t.toString();if(typeof t=="undefined")return"undefined";if(typeof t=="string")return t;try{return JSON.stringify(t)}catch(a){if(a instanceof TypeError)return typeof t=="bigint"?`${t.toString()} (bigint)`:"circular object";throw a}};throw new TypeError(`Internal Error: encountered impossible value "${n(e)}"`)};function c(e){return Object.prototype.hasOwnProperty.call(e,"_")}function r(e,n,t){if(typeof t!="undefined")return r.tag(e,n,t);let a=e,u=n;if(!Object.prototype.hasOwnProperty.call(u,a)){let x=a;return c(u)?u._(x):o(x)}let s=u[a];return s(a)}r.tag=(e,n,t)=>{let a=e[n];if(!Object.prototype.hasOwnProperty.call(t,a)){let s=e;return c(t)?t._(s):o(s)}let p=t[a];return p(e)};0&&(module.exports={corrupt,exhaustive}); |
{ | ||
"name": "exhaustive", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Exhaustiveness checking in TypeScript", | ||
@@ -40,25 +40,23 @@ "author": "Luke Morales <lukemorales@live.com>", | ||
"devDependencies": { | ||
"@changesets/changelog-github": "^0.4.8", | ||
"@changesets/cli": "^2.26.0", | ||
"@typescript-eslint/eslint-plugin": "^5.49.0", | ||
"@typescript-eslint/parser": "^5.49.0", | ||
"@vitest/coverage-c8": "^0.27.3", | ||
"@vitest/ui": "^0.27.3", | ||
"eslint": "^8.32.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-config-prettier": "^8.6.0", | ||
"eslint-import-resolver-typescript": "^3.5.3", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-import-helpers": "^1.3.1", | ||
"eslint-plugin-jest": "^27.2.1", | ||
"eslint-plugin-jest-formatting": "^3.1.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"eslint-plugin-simple-import-sort": "^9.0.0", | ||
"@changesets/changelog-github": "^0.5.0", | ||
"@changesets/cli": "^2.27.1", | ||
"@lukemorales/prettier-config": "^1.1.1", | ||
"@vitest/coverage-v8": "^1.5.0", | ||
"@vitest/ui": "^1.5.0", | ||
"eslint": "^8.56.0", | ||
"eslint-config-lukemorales": "^0.5.0", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^2.8.3", | ||
"rimraf": "^4.1.1", | ||
"tsup": "^6.5.0", | ||
"typescript": "^4.9.4", | ||
"vitest": "^0.27.3" | ||
"prettier": "^3.2.5", | ||
"rimraf": "^5.0.5", | ||
"tsup": "^8.0.2", | ||
"typescript": "^5.4.5", | ||
"vitest": "^1.5.0" | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"lukemorales/oss", | ||
"lukemorales/jest" | ||
] | ||
}, | ||
"prettier": "@lukemorales/prettier-config", | ||
"scripts": { | ||
@@ -74,2 +72,4 @@ "build": "run-p build:*", | ||
"lint:tsc": "tsc --project tsconfig.json --noEmit", | ||
"lint:format": "prettier --check ./src", | ||
"lint:format:fix": "prettier --write ./src", | ||
"test": "vitest --passWithNoTests", | ||
@@ -76,0 +76,0 @@ "test:ci": "pnpm run test:coverage --run", |
Sorry, the diff of this file is not supported yet
17189
13
7
66