LearningTypeScript / projects

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

🐛 Bug: [08-classes-horror-factory] tsc errors #276

Closed vimode closed 1 year ago

vimode commented 1 year ago

Bug Report Checklist

Expected

tsc cli should check for errors in index.ts

test script should run all the tests in the project

Actual

There are multiple issues with this project and I am unsure how to help to fix them.

TSC compilation(?) error

npm run tsc -- --watch

The command when run inside the project directory as instructed shows errors from index.test.ts file instead of showing it for index.ts

Excerpt of the CLI output

npm run tsc -- --watch
> tsc                                                                                
> tsc --watch
Starting compilation in watch mode...
src/index.test.ts:27:19 - error TS2345: Argument of type 'Horror | Horror' is not assignable to 
parameter of type 'Horror & Horror'.
Type 'Horror' is not assignable to type 'Horror & Horror'.

Found 18 errors. Watching for file changes.

When I add the arg to just check the src/solution.ts it shows the following error

error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher.

Found 3 errors in the same file.

The config seems to be targeting es2021 but it still shows that error, not sure why.

Running tests

npm run test -- --watch

When I run the test, it will show 'No tests found' but pressing 'a' will run all the tests properly.

I hope I was able to explain the issues and you are able to check those, if you need more information please let me know I will try my best to provide all the information needed.

Impacted Project

/classes/horror-factory/

https://github.com/LearningTypeScript/projects/tree/main/projects/classes/horror-factory

Additional Info

No response

JoshuaKGoldberg commented 1 year ago

Ah, this is an interesting issue. Thanks for filing!

The Horror | Horror issue is, as the last line of the type error suggests, because the class Horror one would write in src/index.ts is different from the class Horror already written in src/solution.ts.:

Property '#consumed' in type 'Horror' refers to a different member that cannot be accessed from within type 'Horror'.

As for how to fix it... hmm. Tricky. I'll work on this.

When I add the arg to just check the src/solution.ts it shows the following error

error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher.
...

Ah, did you specify a file to get that? npm run tsc -- src/solution.ts or similar? tsc will not reading from tsconfig.json in that case, and instead uses the default compiler options. Which as of 2023 are still older than ECMAScript 2015.

JoshuaKGoldberg commented 1 year ago

When I run the test, it will show 'No tests found' but pressing 'a' will run all the tests properly.

Does it specifically say:

No tests found related to files changed since last commit.
Press `a` to run all tests, or run Jest with `--watchAll`.

If so: yes, that's Jest's default behavior.

JoshuaKGoldberg commented 1 year ago

I believe this should be fixed by #277. But please post back if not and I can take another look!

vimode commented 1 year ago

Everything works as it should! Thanks for the quick fix.

Thank you for taking the time to explain the issue but I guess I need to understand a lot more about types and testing to understand the fix. These projects have been very helpful.