WebStorm not showing errors for some typed assignment while TypeScript & VSCode does
Please look at simple example with unreported typing problems
WebStorm

VSCode handles all of them correctly:

tsc output
lib/main.js(11,9): error TS2339: Property 'abc' does not exist on type 'Owner'.
lib/main.js(13,3): error TS2322: Type '1' is not assignable to type 'string'.
lib/main.js(14,11): error TS2339: Property 'zzz' does not exist on type 'Car'.
lib/main.js(18,3): error TS2322: Type 'number[]' is not assignable to type 'Car[]'.
Type 'number' is not assignable to type 'Car'.
lib/main.js(19,3): error TS2322: Type 'number[]' is not assignable to type 'Car[]'.
lib/main.js(22,3): error TS2322: Type '1' is not assignable to type 'string'.
lib/main.js(24,3): error TS2322: Type 'string' is not assignable to type 'boolean'.
lib/main.js(27,14): error TS2345: Argument of type '"a"' is not assignable to parameter of type 'Owner'.
Example project uploaded via web form, ws_types.zip
P.S. Probably I just have to turn on some inspection rule? If yes, which ones?
WebStorm 2017.2.3
Build #WS-172.3968.27, built on August 29, 2017
Licensed to Ihor Halchevskyi
Subscription is active until November 22, 2017
JRE: 1.8.0_152-release-915-b11 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.12.4
Please sign in to leave a comment.
Well, I'd say this is arguable - JavaScript allows re-assigning vars to a value of different type, and `obj.x = value` is a valid way to create a new property on obj...
For example, the following code will work fine:
var Owner = function (id, name, cars) {
this.id = id
this.name = name
this.cars = cars
}
var Car = function (id, md, desc) {
this.id = id
this.model = md
this.description = desc
}
/**
* @param {Owner} owner
* @returns {boolean}
*/
function processOwner(owner) {
let name = owner.name
let id = owner.id
const cars = owner.cars
cars[0].zzz = 33; //not reported by Webstorm
const carsIds = owner.cars.map(car => car.id)
owner.cars = carsIds // <-- NOT REPORTED BY WebStorm, Should be: "Type 'number[]' is not assignable to type 'Car[]'"
name = 1 // not reported by WebStorm
console.log(name + " owns " + cars[0].model + " has id: " + owner.cars[0] + " where zzz is: " + cars[0].zzz)
}
let car1 = new Car(1, "ford", " first car")
let car2 = new Car(2, "lada", " second car")
let cars = [car1, car2]
let own = new Owner(1, "Jon", cars)
processOwner(own)
you won't see any runtime errors, and result of execution would be:
1 owns ford has id: 1 where zzz is: 33
Hi Elena, you are right, but point is that when JS developer wants to have predictability he uses types. Otherwise we can go with forgiving mode where any known & unknown property could be anything that you described. So when I'm in typed mode and type says that Car does not have attribute 'zzz'
its expected to be reported by IDE (tsc, VSCode does it)
Now let's take a look at line 18 - WebStorm does actually report it correctly
But then the very next line is not reported. So now it's double standards - hardcoded item [1,2,3] is treated as error, but "carsIds" is not error. I know you might say that it is a final hardcoded value but not a variable. Here is another example that shows that it actually works in WebStorm when coded this way
And finally the last one on line 22 in original screenshot where VSCode reports an error and WebStorm does not. It could be fixed by extra JSDoc so that WebStorm would report it as an error.
But it would be a mess, not the code, if typed comments will all over the place, while it could be inferred from function params instead. In addition As you can see from other examples that WebStorm already able to perform type inference, but the errors found when types don't match in these cases are not reported for some reason, but are reported in tsc & VSCode by default.
That's why I asked in the first place that I might need to turn on some inspection rules in order to get errors/warnings in WebStorm I'm looking for.
@Elena Pogorelova, any news?
Please be patient:) This is just a community forum - so there are no obligations or guarantees regarding response time. Do you really expect anybody to reply on Sunday?
>So when I'm in typed mode and type says that Car does not have attribute 'zzz'
well, WebStorm does respect typed mode - for those variables that have the types specified explicitly. but not for new variables that don't have type info specified via JSDoc.
So
is reported, but
is not, despite the fact that the type is correctly inferred by IDE in both cases. WebStorm knows the cars[] type, but allows you to change it silently, as no type is specified explicitly for const cars. Same is true for
owner.name = 1vs
as for
owner.cars = carsIdsWebStorm doesn't evaluate map() during static code analysis and thus doesn't know what
owner.cars.map(car => car.id)is, thus no error is shown
> Please be patient:) This is just a community forum - so there are no obligations or guarantees regarding response time. Do you really expect anybody to reply on Sunday?
My bad, long weekend got me, I thought it's Monday already :)
> well, WebStorm does respect typed mode - for those variables that have the types specified explicitly. but not for new variables that don't have type info specified via JSDoc.
It would be great to have it implemented in WebStorm. VSCode have it out of the box, so WebStorm should have this feature as well.
> owner.cars = carsIds
> WebStorm doesn't evaluate map() during static code analysis and thus doesn't know what
But for code completion it does evaluation and I'm able to have very handy code completion using TypeScript interfaces, types, enums, etc.. declared in JSDoc.
WebStorm already uses TypeScript service for code completion and Angular, the next logical step should be this type guarding. It would help so many developers that use IntelliJ products for writing JS be much more productive.
BTW, how to add quotation formatting on this forum? :)
>WebStorm already uses TypeScript service for code completion and Angular, the next logical step should be this type guarding
In 2017.3, WebStorm will use TypeScript service for JS if
"allowJs": trueThat's awesome! Thanks Elena