Skip to content
Myr

spec_ts.ts

This file

Path

__tests__/spec_ts.ts

56 lines · 1.7 KiB · TypeScript

Last changed by

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

History of this file

Take it

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

lines 1–56why, 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) : 02021export type Test<Left, Right> = IsAny<Left> extends 122 ? IsAny<Right> extends 123 ? 124 : "❌ Left type is 'any' but right type is not"25 : IsAny<Right> extends 126 ? "❌ Right type is 'any' but left type is not"27 : [Left] extends [Right]28 ? [Right] extends [Left]29 ? Any extends TestExact<Left, Right>30 ? 131 : "❌ Unexpected or missing 'readonly' property"32 : "❌ Right type is not assignable to left type"33 : "❌ Left type is not assignable to right type"3435type Assert<T, U> = U extends 136 ? T // No error.37 : IsAny<T> extends 138 ? never // Ensure "any" is refused.39 : U // Return the error message.4041/**42 * Raise a compiler error when both argument types are not identical.43 */44export const assert: <Left, Right>(45 left: Assert<Left, Test<Left, Right>>,46 right: Assert<Right, Test<Left, Right>>47) => Right = x => x as any4849/**50 * Placeholder value followed by "as T"51 */52export const _: any = Symbol("spec.ts placeholder")5354test("empty test to silence jest", () => {55 expect(true).toBeTruthy()56})