Partial<Car>
is identified as a vulnerable type due to all its properties being optional. In TypeScript 2.4, a validation was introduced to alert users when attempting to assign something like a string
to a weak type because of the absence of matching properties:
let car: { make?: string, model?: string };
car = {}; // permissible
car = "oops"; // error! Type '"oops"' has no properties in common with type
// '{ make?: string | undefined; model?: string | undefined; }'.
Prior to TypeScript 2.4, assigning car = "oops"
would not trigger an error since none of the visible features of "oops"
(like length
and toUpperCase
) conflict with Partial<Car>
. From a structural perspective, a string
basically qualifies as a Partial<Car>
. Nonetheless, this assignment is most likely erroneous and the weak type detection cautions against it.
It appears that this weak type detection exclusively pertains to assignability and doesn't come into play for comparison operators, as you have observed:
if (car === "oops") { } // no error
An enhancement request remains open at microsoft/TypeScript#32627 seeking a modification in this aspect. If this holds significance for you and you wish for a change, visiting the issue link, showing support with a thumbs up 👍, and presenting a compelling argument could be beneficial. Nevertheless, the impact might be limited given the lack of community engagement on this matter. Presumably, instances of encountering such scenarios in real-world code are infrequent.
To address your initial inquiry: weak type detection was never integrated for comparison operators, and the demand for altering this seems minimal.
Playground link showcasing code