Skip to content

Inconsistent missing await in conditional errors, invalid quick fix #43514

@bagdonas

Description

@bagdonas

πŸ”Ž Search Terms

  • missing await in conditional
  • await
  • conditional

πŸ•— Version & Regression Information

Since 4.3.0-dev

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

declare const cache: Record<string, Promise<string>>

declare const fetchThing: (id: string) => Promise<string>

const getCachedThing1 = (id: string) => {
  let thing = cache[id]

  return !thing // not an error, if the error in getCachedThing3 is valid then why this isn't an error?
    ? (cache[id] = fetchThing(id))
    : thing
}

const getCachedThing2 = (id: string) => {
  let thing = cache[id]

  return thing ?? (cache[id] = fetchThing(id)) // not an error
}

const getCachedThing3 = (id: string) => {
  let thing = cache[id]

  return thing // error (2801), but it should't be, getCachedThing3 isn't async function
    ? thing
    : (cache[id] = fetchThing(id))
}

After applying quick fix to getChachedThing3 error:

const getCachedThing3 = (id: string) => {
  let thing = cache[id]

  return await thing // error (2801), but it should't be, getCachedThing3 isn't async function
 // error (2801), but it should't be, getCachedThing3 isn't async function
    ? thing
    : (cache[id] = fetchThing(id))
}

Quick fixed code is invalid (await is added to non-async function). Also, comment gets duplicated.

πŸ™ Actual behavior

  1. Missing await in conditional errors are inconsistent
  2. Missing await in conditional errors get reported in non-async functions
  3. Quick fixing the error in non-async function produces invalid results

πŸ™‚ Expected behavior

  1. Missing await in conditional errors to be consistent
  2. Missing await in conditional errors to NOT be reported in non-async functions
  3. No quick fix suggestion in non-async function

As a sidenote, I think the "missing await in conditional error" feature should be under a flag

  1. It is wildly inconsistent
  2. This is a job for linters
  3. Objects are thruthy, so let them be thruthy :)
  4. This feature breaks existing code

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions