"Variable is used before being assigned" even though it is assigned
See original GitHub issueTypeScript 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:
- Created 6 years ago
- Comments:17 (9 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:Try it on TS Playground
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.