You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
enumX{A='A'}functionx(res: {action: string}):boolean{returnres===X.A// error}functiony(res: {action: string|undefined}):boolean{returnres===X.A// error}functionz(res: {action?: string}):boolean{returnres===X.A// no error, but always false}functioncorrect(res: {action: string}):boolean{returnres.action===X.A// what the code should have been}
Expected behavior:
The comparison in function z should have resulted in error TS2367: This condition will always return 'false' since the types '{ action?: string; }' and 'X.A' have no overlap.
Actual behavior:
Functions x, y give the correct error. Function z has no compiler error, but the condition will always evaluate to false since it is comparing an object and a string.
It's an interesting question as to whether or not comparability checks should actually be exempt from weak type checks. I'm usually pretty conservative on this stuff, but my intuition tells me this would not break that much.
I recently ran across some broken code in a repo that lead me here. Basically my situation was something like the following:
interfaceCar{make: stringmodel: string}typeSomeType=Partial<Car>constcar: SomeType={}if(car==="typescript doesn't complain about this"){// This code will never execute..?}
Weak typing is clearly giving us a false sense of security about this code by not warning us that there's no overlap between the partial type and a string. Would love for Typescript to detect these types of errors in the future.
TypeScript Version: 3.6.0-dev.20190730 (also with 3.5.1)
Search Terms: compare types, comparable, always false comparison, optional property, properties
Code
Expected behavior:
The comparison in function
z
should have resulted inerror TS2367: This condition will always return 'false' since the types '{ action?: string; }' and 'X.A' have no overlap.
Actual behavior:
Functions
x
,y
give the correct error. Functionz
has no compiler error, but the condition will always evaluate to false since it is comparing an object and a string.Playground Link: https://www.typescriptlang.org/play/#code/KYOwrgtgBAGlDeBYAUFNUCCBeKByDuKAviigGZggDGALgJYD2IUAHgBQBOwAzgFwIBDWoxD9uNDnRABzIgEpeAIwYMANsAHMkyLjTAdmXblCynYAOgxQA9NajAOHBh2KlkFavSZQAnpx788EJeolDikjJQAD5QlAAmwGRSwHHySirqmggouvqGPCZmMJY2dg5OLsgkyOSUwt4AXv58gvUgAPxiElKyCspqGlo5wHoGUEaFOMVWtlAgDPaOzgA0UIpgNFACqgDuAj7GZNvcwK417nUhUFTOXLTNgcEiXRG96QNZ2rljRuZP3qYpiVZjsABYCTY0UHAa4MBJhUEMMCqOJQcEANxhimAoDOQA
Related Issues: n/a
The text was updated successfully, but these errors were encountered: