plasma-umass / cwhy

"See why!" Explains and suggests fixes for compile-time errors for C, C++, C#, Go, Java, LaTeX, PHP, Python, Ruby, Rust, and TypeScript
Apache License 2.0
273 stars 6 forks source link

Added TypeScript support. #20

Closed emeryberger closed 1 year ago

emeryberger commented 1 year ago

Note that this does not work if --pretty is used, or if pretty is set to True in a tsconfig.json project file.

I tested this with csrankings.ts (https://github.com/emeryberger/CSrankings/blob/4eaf0350efb9d95715ef92395f81e7be8b8662a0/csrankings.ts#L256), which I changed to produce a type error:

-    public static readonly parentMap: { [key: string]: string }
+    public static readonly parentMap: { [key: string]: number }

The result from cwhy, which is pretty good in that it points to the type error (and a lot better than the 80 lines of errors that look like this):

Original error:

csrankings.ts(258,13): error TS2322: Type 'string' is not assignable to type 'number'.
csrankings.ts(259,13): error TS2322: Type 'string' is not assignable to type 'number'.
csrankings.ts(260,13): error TS2322: Type 'string' is not assignable to type 'number'.
csrankings.ts(261,13): error TS2322: Type 'string' is not assignable to type 'number'.
csrankings.ts(262,13): error TS2322: Type 'string' is not assignable to type 'number'.

CWhy:

% tsc --project tsconfig.json |& cwhy
The problem is that you are trying to assign a string value to variables that are declared as having a number type. The error messages are indicating the lines where these type mismatches occur.

For example, in line 258:

258     'aaai': 'ai',

You are assigning the value 'ai' (a string) to the key 'aaai' in the parentMap object, which has a number type. This is causing the error.

To fix this issue, you should ensure that the values assigned to the keys in parentMap are numbers instead of strings. For example, you can modify line 258 to:

258     'aaai': 1,

And do the same for all other lines where string values are assigned to numerical keys.