Skip to content
Myr

spec_ts.ts

This file

Path

__tests__/spec_ts.ts

57 lines · 1.7 KiB · TypeScript

Last changed by

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

History of this file

Take it

chore: remove dependency on spec.ts to fix build issue (#1104)Michel Weststratebefore adoption44363f7

lines 1–20why, one step back
1/**
2 * This file is literally copied from https://github.com/aleclarson/spec.ts/blob/master/index.d.ts.3 * For the sole reason, that the package somehow fails to install in our GitHub workflow.4 * It is unclear why, but all credits to @aleclarson!5 */67// Give "any" its own class8export class Any {9 // @ts-ignore10 private _: true11}1213// Conditional returns can enforce identical types.14// See here: https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-42152965015// prettier-ignore16type TestExact<Left, Right> =17 (<U>() => U extends Left ? 1 : 0) extends (<U>() => U extends Right ? 1 : 0) ? Any : never;1819type IsAny<T> = Any extends T ? ([T] extends [Any] ? 1 : 0) : 020

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

lines 21–23why, one step back
21export type Test<Left, Right> =
22 IsAny<Left> extends 123 ? IsAny<Right> extends 1

chore: remove dependency on spec.ts to fix build issue (#1104)Michel Weststratebefore adoption44363f7

line 24why, one step back
24 ? 1

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

lines 25–34why, one step back
25 : "❌ Left type is 'any' but right type is not"
26 : IsAny<Right> extends 127 ? "❌ Right type is 'any' but left type is not"28 : [Left] extends [Right]29 ? [Right] extends [Left]30 ? Any extends TestExact<Left, Right>31 ? 132 : "❌ Unexpected or missing 'readonly' property"33 : "❌ Right type is not assignable to left type"34 : "❌ Left type is not assignable to right type"

chore: remove dependency on spec.ts to fix build issue (#1104)Michel Weststratebefore adoption44363f7

lines 35–38why, one step back
35
36type Assert<T, U> = U extends 137 ? T // No error.38 : IsAny<T> extends 1

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

lines 39–40why, one step back
39 ? never // Ensure "any" is refused.
40 : U // Return the error message.

chore: remove dependency on spec.ts to fix build issue (#1104)Michel Weststratebefore adoption44363f7

lines 41–57why, one step back
41
42/**43 * Raise a compiler error when both argument types are not identical.44 */45export const assert: <Left, Right>(46 left: Assert<Left, Test<Left, Right>>,47 right: Assert<Right, Test<Left, Right>>48) => Right = x => x as any4950/**51 * Placeholder value followed by "as T"52 */53export const _: any = Symbol("spec.ts placeholder")5455test("empty test to silence jest", () => {56 expect(true).toBeTruthy()57})