diff --git a/__tests__/draft.ts b/__tests__/draft.ts index 52d15996..b8155df7 100644 --- a/__tests__/draft.ts +++ b/__tests__/draft.ts @@ -1,4 +1,4 @@ -import {assert, _} from "spec.ts" +import {assert, _} from "./spec_ts" import {produce, Draft, castDraft, original} from "../src/immer" // For checking if a type is assignable to its draft type (and vice versa) diff --git a/__tests__/immutable.ts b/__tests__/immutable.ts index 1e8ec9be..a0941638 100644 --- a/__tests__/immutable.ts +++ b/__tests__/immutable.ts @@ -1,4 +1,4 @@ -import {assert, _} from "spec.ts" +import {assert, _} from "./spec_ts" import {produce, Immutable, castImmutable} from "../src/immer" test("types are ok", () => { diff --git a/__tests__/produce.ts b/__tests__/produce.ts index c5074216..22f0a318 100644 --- a/__tests__/produce.ts +++ b/__tests__/produce.ts @@ -1,4 +1,4 @@ -import {assert, _} from "spec.ts" +import {assert, _} from "./spec_ts" import { produce, applyPatches, diff --git a/__tests__/redux.ts b/__tests__/redux.ts index 0f51f52c..d5dc2343 100644 --- a/__tests__/redux.ts +++ b/__tests__/redux.ts @@ -1,4 +1,4 @@ -import {assert, _} from "spec.ts" +import {assert, _} from "./spec_ts" import {produce, Draft} from "../src/immer" import * as redux from "redux" diff --git a/__tests__/spec_ts.ts b/__tests__/spec_ts.ts new file mode 100644 index 00000000..b8debafa --- /dev/null +++ b/__tests__/spec_ts.ts @@ -0,0 +1,56 @@ +/** + * This file is literally copied from https://github.com/aleclarson/spec.ts/blob/master/index.d.ts. + * For the sole reason, that the package somehow fails to install in our GitHub workflow. + * It is unclear why, but all credits to @aleclarson! + */ + +// Give "any" its own class +export class Any { + // @ts-ignore + private _: true +} + +// Conditional returns can enforce identical types. +// See here: https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650 +// prettier-ignore +type TestExact = + (() => U extends Left ? 1 : 0) extends (() => U extends Right ? 1 : 0) ? Any : never; + +type IsAny = Any extends T ? ([T] extends [Any] ? 1 : 0) : 0 + +export type Test = IsAny extends 1 + ? IsAny extends 1 + ? 1 + : "❌ Left type is 'any' but right type is not" + : IsAny extends 1 + ? "❌ Right type is 'any' but left type is not" + : [Left] extends [Right] + ? [Right] extends [Left] + ? Any extends TestExact + ? 1 + : "❌ Unexpected or missing 'readonly' property" + : "❌ Right type is not assignable to left type" + : "❌ Left type is not assignable to right type" + +type Assert = U extends 1 + ? T // No error. + : IsAny extends 1 + ? never // Ensure "any" is refused. + : U // Return the error message. + +/** + * Raise a compiler error when both argument types are not identical. + */ +export const assert: ( + left: Assert>, + right: Assert> +) => Right = x => x as any + +/** + * Placeholder value followed by "as T" + */ +export const _: any = Symbol("spec.ts placeholder") + +test("empty test to silence jest", () => { + expect(true).toBeTruthy() +}) diff --git a/package.json b/package.json index 445c6641..ee4cd40f 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,6 @@ "rimraf": "^2.6.2", "seamless-immutable": "^7.1.3", "semantic-release": "^17.0.2", - "spec.ts": "^1.1.0", "ts-jest": "^29.0.0", "tsup": "^6.7.0", "typescript": "^5.0.2" diff --git a/yarn.lock b/yarn.lock index a1752d03..9814b566 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8301,11 +8301,6 @@ spdx-license-ids@^3.0.0: resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-3.0.13.tgz#7189a474c46f8d47c7b0da4b987bb45e908bd2d5" integrity sha512-XkD+zwiqXHikFZm4AX/7JSCXA98U5Db4AFd5XUg/+9UNtnH75+Z9KxtpYiJZx36mUDVOwH83pl7yvCer6ewM3w== -spec.ts@^1.1.0: - version "1.1.3" - resolved "https://registry.yarnpkg.com/spec.ts/-/spec.ts-1.1.3.tgz#2951ed40a984386fdd5bb287719e67a1ceaafff1" - integrity sha512-xDzHAwbHqe9OIHT1c+pnVpVuYSHNC5vk51aFKKoql2aH1RCzHplkd7MoSV7uLfzgm3GmlfpAfwLtfH2T3lQMmw== - split-string@^3.0.1, split-string@^3.0.2: version "3.1.0" resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2"