{"name":"expect-type","version":"0.14.0-0","keywords":["typescript","type-check","assert","types","typings","test","testing"],"homepage":"https://github.com/mmkal/ts/tree/main/packages/expect-type#readme","repository":{"type":"git","url":"git+https://github.com/mmkal/expect-type.git"},"license":"Apache-2.0","main":"dist/index.js","types":"dist/index.d.ts","scripts":{"eslint":"eslint","lint":"tsc && eslint .","build":"tsc -p tsconfig.lib.json","test":"tsc"},"devDependencies":{"eslint":"8.23.0","eslint-plugin-mmkal":"0.0.1-2","typescript":"4.8.2"},"readme":"# expect-type\n\nCompile-time tests for types. Useful to make sure types don't regress into being overly-permissive as changes go in over time.\n\nSimilar to Jest's `expect`, but with type-awareness. Gives you access to a number of type-matchers that let you make assertions about the form of a reference or generic type parameter.\n\n```typescript\nimport {foo, bar} from '../foo'\nimport {expectTypeOf} from 'expect-type'\n\ntest('foo types', () => {\n  // make sure `foo` has type {a: number}\n  expectTypeOf(foo).toMatchTypeOf<{a: number}>()\n\n  // make sure `bar` is a function taking a string:\n  expectTypeOf(bar).parameter(0).toBeString()\n  expectTypeOf(bar).returns.not.toBeAny()\n})\n```\n\nIt can be used in your existing test files - or any other type-checked file you'd like - it's built into existing tooling with no dependencies. No extra build step, cli tool, IDE extension, or lint plugin is needed. Just import the function and start writing tests. Failures will be at compile time - they'll appear in your IDE and when you run `tsc`.\n\nSee below for lots more examples.\n\n## Contents\n<!-- codegen:start {preset: markdownTOC, minDepth: 2, maxDepth: 5} -->\n- [Contents](#contents)\n- [Installation and usage](#installation-and-usage)\n- [Documentation](#documentation)\n   - [Features](#features)\n   - [Within test frameworks](#within-test-frameworks)\n      - [Jest & `eslint-plugin-jest`](#jest--eslint-plugin-jest)\n- [Similar projects](#similar-projects)\n   - [Comparison](#comparison)\n<!-- codegen:end -->\n\n## Installation and usage\n\n```cli\nnpm install expect-type\n```\n\n```typescript\nimport {expectTypeOf} from 'expect-type'\n```\n\n## Documentation\n\nThe `expectTypeOf` method takes a single argument, or a generic parameter. Neither it, nor the functions chained off its return value, have any meaningful runtime behaviour. The assertions you write will be _compile-time_ errors if they don't hold true.\n\n### Features\n\n<!-- codegen:start {preset: markdownFromTests, source: test/index.test.ts} -->\nCheck an object's type with `.toEqualTypeOf`:\n\n```typescript\nexpectTypeOf({a: 1}).toEqualTypeOf<{a: number}>()\n```\n\n`.toEqualTypeOf` can check that two concrete objects have equivalent types:\n\n```typescript\nexpectTypeOf({a: 1}).toEqualTypeOf({a: 1})\n```\n\n`.toEqualTypeOf` succeeds for objects with different values, but the same type:\n\n```typescript\nexpectTypeOf({a: 1}).toEqualTypeOf({a: 2})\n```\n\n`.toEqualTypeOf` fails on extra properties:\n\n```typescript\n// @ts-expect-error\nexpectTypeOf({a: 1, b: 1}).toEqualTypeOf<{a: number}>()\n```\n\nTo allow for extra properties, use `.toMatchTypeOf`. This checks that an object \"matches\" a type. This is similar to jest's `.toMatchObject`:\n\n```typescript\nexpectTypeOf({a: 1, b: 1}).toMatchTypeOf({a: 1})\n```\n\nAnother example of the difference between `.toMatchTypeOf` and `.toEqualTypeOf`, using generics. `.toMatchTypeOf` can be used for \"is-a\" relationships:\n\n```typescript\ntype Fruit = {type: 'Fruit'; edible: boolean}\ntype Apple = {type: 'Fruit'; name: 'Apple'; edible: true}\n\nexpectTypeOf<Apple>().toMatchTypeOf<Fruit>()\n\n// @ts-expect-error\nexpectTypeOf<Fruit>().toMatchTypeOf<Apple>()\n\n// @ts-expect-error\nexpectTypeOf<Apple>().toEqualTypeOf<Fruit>()\n```\n\nAssertions can be inverted with `.not`:\n\n```typescript\nexpectTypeOf({a: 1}).not.toMatchTypeOf({b: 1})\n```\n\n`.not` can be easier than relying on `// @ts-expect-error`:\n\n```typescript\ntype Fruit = {type: 'Fruit'; edible: boolean}\ntype Apple = {type: 'Fruit'; name: 'Apple'; edible: true}\n\nexpectTypeOf<Apple>().toMatchTypeOf<Fruit>()\n\nexpectTypeOf<Fruit>().not.toMatchTypeOf<Apple>()\nexpectTypeOf<Apple>().not.toEqualTypeOf<Fruit>()\n```\n\nCatch any/unknown/never types:\n\n```typescript\nexpectTypeOf<unknown>().toBeUnknown()\nexpectTypeOf<any>().toBeAny()\nexpectTypeOf<never>().toBeNever()\n\n// @ts-expect-error\nexpectTypeOf<never>().toBeNumber()\n```\n\n`.toEqualTypeOf` distinguishes between deeply-nested `any` and `unknown` properties:\n\n```typescript\nexpectTypeOf<{deeply: {nested: any}}>().not.toEqualTypeOf<{deeply: {nested: unknown}}>()\n```\n\nTest for basic javascript types:\n\n```typescript\nexpectTypeOf(() => 1).toBeFunction()\nexpectTypeOf({}).toBeObject()\nexpectTypeOf([]).toBeArray()\nexpectTypeOf('').toBeString()\nexpectTypeOf(1).toBeNumber()\nexpectTypeOf(true).toBeBoolean()\nexpectTypeOf(() => {}).returns.toBeVoid()\nexpectTypeOf(Promise.resolve(123)).resolves.toBeNumber()\nexpectTypeOf(Symbol(1)).toBeSymbol()\n```\n\nNullable types:\n\n```typescript\nexpectTypeOf(undefined).toBeUndefined()\nexpectTypeOf(undefined).toBeNullable()\nexpectTypeOf(undefined).not.toBeNull()\n\nexpectTypeOf(null).toBeNull()\nexpectTypeOf(null).toBeNullable()\nexpectTypeOf(null).not.toBeUndefined()\n\nexpectTypeOf<1 | undefined>().toBeNullable()\nexpectTypeOf<1 | null>().toBeNullable()\nexpectTypeOf<1 | undefined | null>().toBeNullable()\n```\n\nMore `.not` examples:\n\n```typescript\nexpectTypeOf(1).not.toBeUnknown()\nexpectTypeOf(1).not.toBeAny()\nexpectTypeOf(1).not.toBeNever()\nexpectTypeOf(1).not.toBeNull()\nexpectTypeOf(1).not.toBeUndefined()\nexpectTypeOf(1).not.toBeNullable()\n```\n\nUse `.extract` and `.exclude` to narrow down complex union types:\n\n```typescript\ntype ResponsiveProp<T> = T | T[] | {xs?: T; sm?: T; md?: T}\nconst getResponsiveProp = <T>(_props: T): ResponsiveProp<T> => ({})\ntype CSSProperties = {margin?: string; padding?: string}\n\nconst cssProperties: CSSProperties = {margin: '1px', padding: '2px'}\n\nexpectTypeOf(getResponsiveProp(cssProperties))\n  .exclude<unknown[]>()\n  .exclude<{xs?: unknown}>()\n  .toEqualTypeOf<CSSProperties>()\n\nexpectTypeOf(getResponsiveProp(cssProperties))\n  .extract<unknown[]>()\n  .toEqualTypeOf<CSSProperties[]>()\n\nexpectTypeOf(getResponsiveProp(cssProperties))\n  .extract<{xs?: any}>()\n  .toEqualTypeOf<{xs?: CSSProperties; sm?: CSSProperties; md?: CSSProperties}>()\n\nexpectTypeOf<ResponsiveProp<number>>().exclude<number | number[]>().toHaveProperty('sm')\nexpectTypeOf<ResponsiveProp<number>>().exclude<number | number[]>().not.toHaveProperty('xxl')\n```\n\n`.extract` and `.exclude` return never if no types remain after exclusion:\n\n```typescript\ntype Person = {name: string; age: number}\ntype Customer = Person & {customerId: string}\ntype Employee = Person & {employeeId: string}\n\nexpectTypeOf<Customer | Employee>().extract<{foo: string}>().toBeNever()\nexpectTypeOf<Customer | Employee>().exclude<{name: string}>().toBeNever()\n```\n\nMake assertions about object properties:\n\n```typescript\nconst obj = {a: 1, b: ''}\n\n// check that properties exist (or don't) with `.toHaveProperty`\nexpectTypeOf(obj).toHaveProperty('a')\nexpectTypeOf(obj).not.toHaveProperty('c')\n\n// check types of properties\nexpectTypeOf(obj).toHaveProperty('a').toBeNumber()\nexpectTypeOf(obj).toHaveProperty('b').toBeString()\nexpectTypeOf(obj).toHaveProperty('a').not.toBeString()\n```\n\n`.toEqualTypeOf` can be used to distinguish between functions:\n\n```typescript\ntype NoParam = () => void\ntype HasParam = (s: string) => void\n\nexpectTypeOf<NoParam>().not.toEqualTypeOf<HasParam>()\n```\n\nBut often it's preferable to use `.parameters` or `.returns` for more specific function assertions:\n\n```typescript\ntype NoParam = () => void\ntype HasParam = (s: string) => void\n\nexpectTypeOf<NoParam>().parameters.toEqualTypeOf<[]>()\nexpectTypeOf<NoParam>().returns.toBeVoid()\n\nexpectTypeOf<HasParam>().parameters.toEqualTypeOf<[string]>()\nexpectTypeOf<HasParam>().returns.toBeVoid()\n```\n\nMore examples of ways to work with functions - parameters using `.parameter(n)` or `.parameters`, and return values using `.returns`:\n\n```typescript\nconst f = (a: number) => [a, a]\n\nexpectTypeOf(f).toBeFunction()\n\nexpectTypeOf(f).toBeCallableWith(1)\nexpectTypeOf(f).not.toBeAny()\nexpectTypeOf(f).returns.not.toBeAny()\nexpectTypeOf(f).returns.toEqualTypeOf([1, 2])\nexpectTypeOf(f).returns.toEqualTypeOf([1, 2, 3])\nexpectTypeOf(f).parameter(0).not.toEqualTypeOf('1')\nexpectTypeOf(f).parameter(0).toEqualTypeOf(1)\nexpectTypeOf(1).parameter(0).toBeNever()\n\nconst twoArgFunc = (a: number, b: string) => ({a, b})\n\nexpectTypeOf(twoArgFunc).parameters.toEqualTypeOf<[number, string]>()\n```\n\nYou can also check type guards & type assertions:\n\n```typescript\nconst assertNumber = (v: any): asserts v is number => {\n  if (typeof v !== 'number') {\n    throw new TypeError('Nope !')\n  }\n}\n\nexpectTypeOf(assertNumber).asserts.toBeNumber()\n\nconst isString = (v: any): v is string => typeof v === 'string'\nexpectTypeOf(isString).guards.toBeString()\n```\n\nAssert on constructor parameters:\n\n```typescript\nexpectTypeOf(Date).toBeConstructibleWith('1970')\nexpectTypeOf(Date).toBeConstructibleWith(0)\nexpectTypeOf(Date).toBeConstructibleWith(new Date())\nexpectTypeOf(Date).toBeConstructibleWith()\n\nexpectTypeOf(Date).constructorParameters.toEqualTypeOf<[] | [string | number | Date]>()\n```\n\nClass instance types:\n\n```typescript\nexpectTypeOf(Date).instance.toHaveProperty('toISOString')\n```\n\nPromise resolution types can be checked with `.resolves`:\n\n```typescript\nconst asyncFunc = async () => 123\n\nexpectTypeOf(asyncFunc).returns.resolves.toBeNumber()\n```\n\nArray items can be checked with `.items`:\n\n```typescript\nexpectTypeOf([1, 2, 3]).items.toBeNumber()\nexpectTypeOf([1, 2, 3]).items.not.toBeString()\n```\n\nCheck that functions never return:\n\n```typescript\nconst thrower = () => {\n  throw new Error('oh no')\n}\n\nexpectTypeOf(thrower).returns.toBeNever()\n```\n\nGenerics can be used rather than references:\n\n```typescript\nexpectTypeOf<{a: string}>().not.toEqualTypeOf<{a: number}>()\n```\n\nDistinguish between missing/null/optional properties:\n\n```typescript\nexpectTypeOf<{a?: number}>().not.toEqualTypeOf<{}>()\nexpectTypeOf<{a?: number}>().not.toEqualTypeOf<{a: number}>()\nexpectTypeOf<{a?: number}>().not.toEqualTypeOf<{a: number | undefined}>()\nexpectTypeOf<{a?: number | null}>().not.toEqualTypeOf<{a: number | null}>()\nexpectTypeOf<{a: {b?: number}}>().not.toEqualTypeOf<{a: {}}>()\n```\n\nDetect the difference between regular and readonly properties:\n\n```typescript\ntype A1 = {readonly a: string; b: string}\ntype E1 = {a: string; b: string}\n\nexpectTypeOf<A1>().toMatchTypeOf<E1>()\nexpectTypeOf<A1>().not.toEqualTypeOf<E1>()\n\ntype A2 = {a: string; b: {readonly c: string}}\ntype E2 = {a: string; b: {c: string}}\n\nexpectTypeOf<A2>().toMatchTypeOf<E2>()\nexpectTypeOf<A2>().not.toEqualTypeOf<E2>()\n```\n\nDistinguish between classes with different constructors:\n\n```typescript\nclass A {\n  value: number\n  constructor(a: 1) {\n    this.value = a\n  }\n}\nclass B {\n  value: number\n  constructor(b: 2) {\n    this.value = b\n  }\n}\n\nexpectTypeOf<typeof A>().not.toEqualTypeOf<typeof B>()\n\nclass C {\n  value: number\n  constructor(c: 1) {\n    this.value = c\n  }\n}\n\nexpectTypeOf<typeof A>().toEqualTypeOf<typeof C>()\n```\n<!-- codegen:end -->\n\n### Within test frameworks\n\n#### Jest & `eslint-plugin-jest`\nIf you're using Jest along with `eslint-plugin-jest`, you will get warnings from the [`jest/expect-expect`](https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/expect-expect.md) rule, complaining that \"Test has no assertions\" for tests that only use `expectTypeOf()`.\n\nTo remove this warning, configure the ESlint rule to consider `expectTypeOf` as an assertion:\n\n```js\n\"rules\": {\n  // ...\n  \"jest/expect-expect\": [\n    \"warn\",\n    {\n      \"assertFunctionNames\": [\n        \"expect\", \"expectTypeOf\"\n      ]\n    }\n  ],\n  // ...\n}\n```\n\n## Similar projects\n\nOther projects with similar goals:\n\n- [`tsd`](https://github.com/SamVerschueren/tsd) is a CLI that runs the TypeScript type checker over assertions\n- [`ts-expect`](https://github.com/TypeStrong/ts-expect) exports several generic helper types to perform type assertions\n- [`dtslint`](https://github.com/Microsoft/dtslint) does type checks via comment directives and tslint\n- [`type-plus`](https://github.com/unional/type-plus) comes with various type and runtime TypeScript assertions\n- [`static-type-assert`](https://github.com/ksxnodemodules/static-type-assert) type assertion functions\n\n### Comparison\n\nThe key differences in this project are:\n\n- a fluent, jest-inspired API, making the difference between `actual` and `expected` clear. This is helpful with complex types and assertions.\n- inverting assertions intuitively and easily via `expectTypeOf(...).not`\n- first-class support for:\n  - `any` (as well as `unknown` and `never`) (see issues outstanding at time of writing in tsd for [never](https://github.com/SamVerschueren/tsd/issues/78) and [any](https://github.com/SamVerschueren/tsd/issues/82)).\n    - This can be especially useful in combination with `not`, to protect against functions returning too-permissive types. For example, `const parseFile = (filename: string) => JSON.parse(readFileSync(filename).toString())` returns `any`, which could lead to errors. After giving it a proper return-type, you can add a test for this with `expect(parseFile).returns.not.toBeAny()`\n  - object properties\n  - function parameters\n  - function return values\n  - constructor parameters\n  - class instances\n  - array item values\n  - nullable types\n- assertions on types \"matching\" rather than exact type equality, for \"is-a\" relationships e.g. `expectTypeOf(square).toMatchTypeOf<Shape>()`\n- built into existing tooling. No extra build step, cli tool, IDE extension, or lint plugin is needed. Just import the function and start writing tests. Failures will be at compile time - they'll appear in your IDE and when you run `tsc`.\n- small implementation with no dependencies. <200 lines of code - [take a look!](./src/index.ts) (tsd, for comparison, is [2.6MB](https://bundlephobia.com/result?p=tsd@0.13.1) because it ships a patched version of typescript).\n","readmeFilename":"README.md","gitHead":"a2eff48785712208bd18c7106246e8ddac87bff0","description":"Compile-time tests for types. Useful to make sure types don't regress into being overly-permissive as changes go in over time.","bugs":{"url":"https://github.com/mmkal/expect-type/issues"},"_id":"expect-type@0.14.0-0","_nodeVersion":"16.14.2","_npmVersion":"8.5.0","dist":{"integrity":"sha512-Va0K01LOYZIU5BV1ztxLK3bCKVe7RX6Q2HryYK8A9gnCIKCkcz+6xaccSz9I9pNPTmUFTFT4huZ7S6JCtNudUQ==","shasum":"b02c220948f3c74e8ed2b552ac934e93617f65d0","tarball":"http://registry-npm.ywwl.com/expect-type/download/expect-type-0.14.0-0.tgz","fileCount":5,"unpackedSize":35112,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQD0TTPE2rJ093ZnZViDYrLUh6bPwhAblQrgR5Ddh/6jIQIhAMFtDyzIxPWy4rBKnnbITrV0dAep9xiKfKpX5sMpGd5s"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjFnwtACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmq+dQ/9HgO11eTNLYpdmsneuzKq9PEDk24SDYbF4u2F/Pk4Nn9s+zMv\r\nJwLObhawOOHSHf1LnQ3vlfyCE6HgL78GZNj9TeoLk7nTDf1mILrUdMZbdlQw\r\n5BQcENQpkSjYUiFGTkHd1dm6s/IsaztPoewaFpJt9A14FPm4iniI+uFeBQQb\r\nTS6pMyujVLsdpXRvt/jvPFVcPzGMbiuvysr2qOyxXlGkM6unCDWjxx08ntwg\r\nL7zKQRiPsl1hMUTut9piGIkyTSbBsVygS1GJMra9qwU5yABX0GgNbLNIxgA2\r\nRPG0Dae0QoTEYo0IthTOrLOFMN+BaZCFlPmT/3JiTCA7jhWJxm8MonC/3O9F\r\nWTG/GI/wu9WVaJwVE9SxikDXuhXNAi+Ic5sHffhB00L2ZD777UsHguiW/tlO\r\n9KGGl+ZdxPkGazWuAyZbZU/liCz5glJZ/J3O27kefpueTHE4Z3TyV03201cG\r\nzPS+ZqySht453Pg+RMgnLTtrYNv7cJnSaOZfA6KEvIIYZofY4p+WpDrVo23B\r\nlcqNPygmb+rG3s9O+Uu8mbosl7SscPrOP5MKTZdt8oEomg//mLPs0cYPoYDE\r\nmkzM2+SFLVEENurnEUQAEpNzjJohXY9obMp6DKmcmE8erqcjTLoYqXxhnxRU\r\nMyTKUL9YA+5vsthX6aXrQhctrOGrwUWcUFE=\r\n=BqF/\r\n-----END PGP SIGNATURE-----\r\n","size":10462,"noattachment":false,"key":"/expect-type/-/expect-type-0.14.0-0.tgz"},"_npmUser":{"name":"mmkale","email":"mmkal1@outlook.com"},"directories":{},"maintainers":[{"name":"mmkale","email":"mmkal1@outlook.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/expect-type_0.14.0-0_1662417965740_0.7859506018638616"},"_hasShrinkwrap":false,"publish_time":1662417965923,"_cnpm_publish_time":1662417965923,"dist-tags":{"latest":"1.4.0","master":"0.4.1-master-2020-03-06-00-05-12.11","next":"1.0.0-rc.0"}}