LearningTypeScript / projects

Hands-on real world projects that will help you exercise your knowledge of TypeScript.
https://learningtypescript.com/projects
226 stars 151 forks source link

🐛 Bug: [the-shape-of-types] MockHorror class fails type checking in solution tests #262

Closed JoshuaKGoldberg closed 1 year ago

JoshuaKGoldberg commented 1 year ago

Bug Report Checklist

Expected

npm run test:solutions should pass in all projects, including projects/classes/the-shape-of-types.

Actual

Argument of type 'MockHorror' is not assignable to parameter of type 'Horror & Horror'.
  Type 'MockHorror' is missing the following properties from type 'Horror': #consumed, #consume, doBattle, getPowerts(2345)

Impacted Project

the-shape-of-types > src/index.test.ts

Additional Info

When testing solutions, this happens:

  1. solution.ts is copied to index.ts
  2. The top of index.test.ts is rewritten to:
    const { Demon, Horror, Sorcerer } = process.env.TEST_SOLUTIONS
        ? index
        : // In theory, it would be nice to not have to apply this cast
          // In practice, TypeScript's structural typing does not play well with # privates
          // See https://github.com/LearningTypeScript/projects/issues/183
          (index as unknown as typeof solution);
  3. tsc

At the end, the Horror class is known by TypeScript to be either the import from index or solution. And because Horror has # privates, the two classes aren't assignable to each other.