-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: [no-unnecessary-condition] Unnecessary conditional when trying to use coalesce operator on null | undefined #10055
Comments
The rule is trying to point out that you could write this like function myFunc(arg: null | undefined): undefined {
return undefined
} it reports both always-true and always-false conditions as unnecessary, too, like declare const alwaysTrue: true;
declare const alwaysFalse: false;
// reported by the rule
if (alwaysTrue) {
}
// reported by the rule
if (alwaysFalse) {
} |
Makes sense. My bad but I'm in the process of refactoring a big codebase with eslint-config-love and I'm a bit overflowed by over-reaching rules right now :) |
@kirkwaiblinger sorry for reusing the same issue but I've also noticed the following: let api: Axios
export function useApi(options: AxiosRequestConfig = {}): Axios {
// Unnecessary conditional, the types have no overlap.eslint[@typescript-eslint/no-unnecessary-condition]
if (api == null) {
createApi(options)
}
return api
} Typescript should be able to notice that no value has been assigned for certain to api and thus it can be undefined, right? |
Ah - this is a pet peeve of mine - see #9565. In short - no. |
For future reference, these sort of general usage/support questions are best asked in our discord server 🙂 |
Or, rather - it should complain, but it doesn't! 😆 (note that I have yet to file the TS issue that I intended to file) |
@darkbasic Opened it! microsoft/TypeScript#60064 Thanks for the nudge 🙂 |
Before You File a Bug Report Please Confirm You Have Done The Following...
Playground Link
https://typescript-eslint.io/play/#ts=5.5.2&fileType=.tsx&code=GYVwdgxgLglg9mABAWwJ4DFwQBQEMBOA5gFyJggA2FiAPouACYCmwMYTDAlKYy2x4gDeAKESJ8TKCHxIChRAH4F9MM1bsGwgL5A&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK%2BSpPRgA2uGyRE0aB2iQANKuyYDaofHgcA7gGFJyfEJYAZDh2I3ps%2BU1QZ8cRMYQAL7GALqqIUFAA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false
Repro Code
ESLint Config
tsconfig
Expected Result
arg ?? undefined
limits the union typenull | undefined
to justundefined
so it's not unnecessary.Actual Result
arg ?? undefined
errors out like if it didn't restrict the union type a subset of its values.Additional Info
No response
The text was updated successfully, but these errors were encountered: