question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Variable is used before being assigned" even though it is assigned

See original GitHub issue

TypeScript Version: 2.6.1

Code

type Patch<T> = {
  [P in keyof T]?: Patch<T[P]> | ((value: T[P]) => T[P])
}

function applyPatch<T extends object, K extends keyof T>(target: T, patch: Patch<T>): T {
  (Object.keys(patch) as K[]).forEach(key => {
    const patchValue = patch[key];
    target[key] = typeof patchValue == "object" && !Array.isArray(patchValue)
      ? applyPatch(target[key] || {}, patchValue)
      : typeof patchValue == "function" // Error: Variable 'patchValue' is used before being assigned.
        ? patchValue(target[key])
        : patchValue; // Error: Variable 'patchValue' is used before being assigned.
  });
  return target;
}

Expected behavior: No error

Actual behavior:

Error: Variable ‘patchValue’ is used before being assigned.

It goes away if I assert patchValue as non-null:

const patchValue = patch[key]!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
patrickrobertscommented, Oct 8, 2020

I also have this issue with this code:

let resolver;
new Promise(resolve => (resolver = resolve));
console.log(resolver);

That particular case is a valid warning. Even though we know the executor passed to the Promise constructor is synchronous, TypeScript can’t statically determine that it is and assumes that it is asynchronous. If it were hypothetically asynchronous, the variable would indeed be used before being assigned. You can use an assertion ! to hint to TypeScript that you know it’s assigned at the point of usage:

let resolver: (value?: unknown) => void;
new Promise(resolve => (resolver = resolve));
console.log(resolver!);

Try it on TS Playground

2reactions
andy-mscommented, Feb 8, 2018

I don’t know how hard that would be, but I’ve seen issues similar to this come up in the past and be turned down.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Variable 'test' is used before being assigned - Typescript
I am mapping here one type to another. But vscode shows error that variable 'test' is used before being assigned. can anyone please...
Read more >
Variable is used before assigned, but we're checking whether ...
Declare a variable: let x: number;; Do some complicated logic to possibly assign it; If it wasn't assigned, provide a default value. Currently, ......
Read more >
Variable '<variablename>' is used before it has been assigned ...
Variable '<variablename>' is used before it has been assigned a value. A null reference exception could result at run time.
Read more >
Variable is used before being assigned typescript
A variable must be declared before it is used. Use the var keyword to declare variables. Variable Declaration in TypeScript The type syntax...
Read more >
Documentation - Variable Declaration - TypeScript
const is an augmentation of let in that it prevents re-assignment to a variable. With TypeScript being an extension of JavaScript, the language...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found