Skip to content

📝 Docs: Note that & why Twoslash and $ExpectType have different behavior #74

@OliverJAsh

Description

@OliverJAsh

🐛 Bug Report

  • eslint-plugin-expect-type version: 0.2.2
  • ESLint version: 8.36.0
  • Node version: 16.3.0

Actual Behavior

Full reduced test case here: https://github.com/OliverJAsh/eslint-plugin-expect-type-issue

Contents inlined below.

package.json:

{
  "dependencies": {
    "@typescript-eslint/parser": "^5.55.0",
    "eslint": "^8.36.0",
    "eslint-plugin-expect-type": "^0.2.2",
    "typescript": "^5.0.2"
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "strict": true
  }
}

.eslintrc.js:

const path = require("path");

module.exports = {
  parser: "@typescript-eslint/parser",
  parserOptions: {
    project: path.join(__dirname, "./tsconfig.json"),
  },
  plugins: ["eslint-plugin-expect-type"],
  rules: {
    "expect-type/expect": "error",
  },
};

source.ts:

export type Compact<A> = A extends Function ? A : { [K in keyof A]: A[K] } & {};

test.ts:

import { Compact } from "./source";

declare const any: any;

const x: Compact<Record<"a" | "b", number> | Record<"a" | "c", number>> = any;

// ❌ Error:
//  Expected type to be: { a: number; b: number; } | { a: number; c: number; }, got: Compact<Record<"a" | "b", number> | Record<"a" | "c", number>>
// $ExpectType { a: number; b: number; } | { a: number; c: number; }
type Test1 = typeof x;

// ✅ No error
type Test2 = typeof x;
//   ^? type Test2 = {
//          a: number;
//          b: number;
//      } | {
//          a: number;
//          c: number;
//      }
  • $ExpectType expects: Compact<Record<"a" | "b", number> | Record<"a" | "c", number>>
  • Twoslash expects:
       type Test2 = {
        a: number;
        b: number;
    } | {
        a: number;
        c: number;
    }

I believe the reason for this difference is because Twoslash and $ExpectType use different mechanisms to generate the "actual" value:

This particular issue started after we upgraded to TypeScript 5. Prior to this, $ExpectType had the same behavior as Twoslash in this particular example. I'm not sure why this has changed.

Expected Behavior

Twoslash queries and $ExpectType should behave the same.

Reproduction

https://github.com/OliverJAsh/eslint-plugin-expect-type-issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: documentationImprovements or additions to docs 📝status: accepting prsPlease, send a pull request to resolve this! 🙏

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions