-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Closed
Labels
accepting prsGo ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issuedocumentationDocumentation ("docs") that needs adding/updatingDocumentation ("docs") that needs adding/updatinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.
Description
Before You File a Documentation Request Please Confirm You Have Done The Following...
- I have looked for existing open or closed documentation requests that match my proposal.
- I have read the FAQ and my problem is not listed.
Suggested Changes
const maybe = Math.random() > 0.5 ? '' : undefined;
const definitely = maybe as string;
const alsoDefinitely = <string>maybe;The incorrect" example in the documentation does not report lint error. (playground).
A quick look shows that ts infers the type of maybe as "" | undefined. That's why the as string assertion is allowed because it casts maybe's type ("" -> string)
So, replacing "const" with "let" will cause a lint error.
let maybe = Math.random() > 0.5 ? '' : undefined;
// maybe's type: string | undefined
const definitely = maybe as string; // Lint error
const alsoDefinitely = <string>maybe; // Lint errorIMO, the assertions cast the type ("" -> string) which does not do same things with !, so the documentation example should be fixed. (otherwise it's a false negative)
Affected URL(s)
Metadata
Metadata
Assignees
Labels
accepting prsGo ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issuedocumentationDocumentation ("docs") that needs adding/updatingDocumentation ("docs") that needs adding/updatinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.