-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also removed broken `IfIdenticalInternalTSRepresentation` type.
- Loading branch information
Showing
11 changed files
with
238 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
{ | ||
"rules": { | ||
"unicorn/prevent-abbreviations": "off", | ||
"no-multiple-empty-lines": "off", | ||
"linebreak-style": ["error", "unix"], | ||
"object-curly-spacing": ["error", "always"], | ||
"@typescript-eslint/no-unused-vars": "off", | ||
"@typescript-eslint/no-unsafe-return": "off", | ||
"@typescript-eslint/no-empty-function": "off", | ||
"@typescript-eslint/ban-types": "off", | ||
"@typescript-eslint/indent": "off", | ||
"unicorn/prevent-abbreviations": "off", | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"object-curly-spacing": [ | ||
"error", | ||
"always" | ||
] | ||
"@typescript-eslint/object-curly-spacing": ["error", "always"], | ||
"@typescript-eslint/no-unnecessary-type-arguments": "off", | ||
"@typescript-eslint/no-confusing-void-expression": "off" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,36 @@ | ||
export function tsAssertIsSupertype<A, B extends A>() {} | ||
export function tsAssertTrue<T extends true>() {} | ||
export function tsAssertFalse<T extends false>() {} | ||
import { IfStrictEqual, IfStrictExtends } from '.'; | ||
|
||
const noop = (() => {}); | ||
const identity = ((value: any) => value); | ||
|
||
type Pass = []; | ||
type Fail = [never, never]; | ||
|
||
function tsCreateExactAsserter<Type>(): (<T extends Type = never>(...mismatch: IfStrictEqual<T, Type, Pass, Fail>) => void) { | ||
return noop; | ||
} | ||
|
||
export const tsAssertTypesExactlyEqual = () => tsCreateExactAsserter; | ||
export const tsAssertExtends = () => <A>() => <B>(...mismatch: A extends B ? Pass : Fail) => undefined; | ||
export const tsAssertStrictExtends = () => <A>() => <B>(...mismatch: IfStrictExtends<A, B, Pass, Fail>) => undefined; | ||
export const tsAssertIsAssignable = tsAssertExtends; | ||
|
||
export function tsAssertNever<T extends never>(): void; | ||
export function tsAssertNever(never: never): void; | ||
export function tsAssertNever(never?: any): void {} | ||
|
||
export const tsAssertTrue = tsCreateExactAsserter<true>(); | ||
export const tsAssertFalse = tsCreateExactAsserter<false>(); | ||
export const tsAssertAny = tsCreateExactAsserter<any>(); | ||
export const tsAssertUnknown = tsCreateExactAsserter<unknown>(); | ||
|
||
function tsCreateExactInferAsserter<Type>(): (<T extends Type = never>(value: T, ...mismatch: IfStrictEqual<T, Type, Pass, Fail>) => T) { | ||
return identity as any; | ||
} | ||
|
||
function tsCreateAssignableInferAsserter<Type>(): (<T extends Type = never>(value: T) => T) { | ||
return identity as any; | ||
} | ||
|
||
export const tsAssertTypeExactlyDescribesValue = () => tsCreateExactInferAsserter; | ||
export const tsAssertTypeAcceptsValue = () => tsCreateAssignableInferAsserter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { tsAssertAny, tsAssertUnknown } from '../../source/assert'; | ||
|
||
|
||
tsAssertAny<any>(); | ||
tsAssertUnknown<unknown>(); | ||
|
||
|
||
// @ts-expect-error | ||
tsAssertAny(); | ||
// @ts-expect-error | ||
tsAssertAny<never>(); | ||
// @ts-expect-error | ||
tsAssertAny<void>(); | ||
// @ts-expect-error | ||
tsAssertAny<undefined>(); | ||
// @ts-expect-error | ||
tsAssertAny<boolean>(); | ||
// @ts-expect-error | ||
tsAssertAny<{}>(); | ||
// @ts-expect-error | ||
tsAssertAny<unknown>(); | ||
|
||
|
||
// @ts-expect-error | ||
tsAssertAny(0 as any); // eslint-disable-line @typescript-eslint/no-unsafe-argument | ||
|
||
|
||
// @ts-expect-error | ||
tsAssertUnknown(); | ||
// @ts-expect-error | ||
tsAssertUnknown<never>(); | ||
// @ts-expect-error | ||
tsAssertUnknown<void>(); | ||
// @ts-expect-error | ||
tsAssertUnknown<undefined>(); | ||
// @ts-expect-error | ||
tsAssertUnknown<boolean>(); | ||
// @ts-expect-error | ||
tsAssertUnknown<{}>(); | ||
// @ts-expect-error | ||
tsAssertUnknown<any>(); | ||
|
||
|
||
// @ts-expect-error | ||
tsAssertUnknown(0 as unknown); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { tsAssertNever } from '../../source/assert'; | ||
|
||
|
||
declare const neverValue: never; | ||
tsAssertNever<never>(); | ||
tsAssertNever(neverValue); | ||
tsAssertNever(0 as unknown as never); | ||
|
||
|
||
// @ts-expect-error | ||
tsAssertNever<void>(); | ||
// @ts-expect-error | ||
tsAssertNever<undefined>(); | ||
// @ts-expect-error | ||
tsAssertNever<boolean>(); | ||
// @ts-expect-error | ||
tsAssertNever<{}>(); | ||
// @ts-expect-error | ||
tsAssertNever<unknown>(); | ||
// @ts-expect-error | ||
tsAssertNever<any>(); | ||
|
||
|
||
// @ts-expect-error | ||
tsAssertNever(undefined); | ||
// @ts-expect-error | ||
tsAssertNever(null); | ||
// @ts-expect-error | ||
tsAssertNever({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { tsAssertTrue, tsAssertFalse } from '../../source/assert'; | ||
|
||
|
||
tsAssertTrue<true>(); | ||
tsAssertFalse<false>(); | ||
|
||
|
||
// @ts-expect-error | ||
tsAssertTrue(); | ||
// @ts-expect-error | ||
tsAssertTrue<never>(); | ||
// @ts-expect-error | ||
tsAssertTrue<void>(); | ||
// @ts-expect-error | ||
tsAssertTrue<undefined>(); | ||
// @ts-expect-error | ||
tsAssertTrue<boolean>(); | ||
// @ts-expect-error | ||
tsAssertTrue<{}>(); | ||
// @ts-expect-error | ||
tsAssertTrue<unknown>(); | ||
// @ts-expect-error | ||
tsAssertTrue<any>(); | ||
|
||
|
||
// @ts-expect-error | ||
tsAssertTrue(true); | ||
// @ts-expect-error | ||
tsAssertTrue(true as const); | ||
|
||
|
||
// @ts-expect-error | ||
tsAssertFalse(); | ||
// @ts-expect-error | ||
tsAssertFalse<never>(); | ||
// @ts-expect-error | ||
tsAssertFalse<void>(); | ||
// @ts-expect-error | ||
tsAssertFalse<undefined>(); | ||
// @ts-expect-error | ||
tsAssertFalse<boolean>(); | ||
// @ts-expect-error | ||
tsAssertFalse<{}>(); | ||
// @ts-expect-error | ||
tsAssertFalse<unknown>(); | ||
// @ts-expect-error | ||
tsAssertFalse<any>(); | ||
|
||
|
||
// @ts-expect-error | ||
tsAssertFalse(false); | ||
// @ts-expect-error | ||
tsAssertFalse(false as const); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { tsAssertTypeAcceptsValue, tsAssertTypesExactlyEqual, tsAssertExtends, tsAssertStrictExtends } from '../../source/assert'; | ||
|
||
type T1 = { a: string; b: string | number }; | ||
|
||
const x = tsAssertTypeAcceptsValue()<T1>()( | ||
{ a: 'adfasdf', b: 123 } as const | ||
); | ||
|
||
tsAssertTypesExactlyEqual()<typeof x>()<{ a: 'adfasdf'; b: 123 }>(); | ||
tsAssertExtends()<typeof x>()<T1>(); | ||
tsAssertStrictExtends()<typeof x>()<T1>(); | ||
|
||
// @ts-expect-error | ||
tsAssertTypesExactlyEqual()<typeof x>()<T1>(); | ||
// @ts-expect-error | ||
tsAssertExtends()<T1>()<typeof x>(); | ||
// @ts-expect-error | ||
tsAssertStrictExtends()<T1>()<typeof x>(); | ||
|
||
const y = tsAssertTypeAcceptsValue()<T1>()( | ||
{ a: 'adfasdf', b: 123 } | ||
); | ||
|
||
tsAssertTypesExactlyEqual()<typeof y>()<{ a: string; b: number }>(); | ||
tsAssertExtends()<typeof y>()<T1>(); | ||
tsAssertStrictExtends()<typeof y>()<T1>(); | ||
|
||
// @ts-expect-error | ||
tsAssertTypesExactlyEqual()<typeof y>()<T1>(); | ||
// @ts-expect-error | ||
tsAssertExtends()<T1>()<typeof y>(); | ||
// @ts-expect-error | ||
tsAssertStrictExtends()<T1>()<typeof y>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters