3
if (!challengeType) {
   const { brawl, fight }: any = await userModel
                                          .findOneAndUpdate(
                                               { _id: req.userData._id },
                                               { $inc: { 'fight.remaining': -1 } },
                                               { 'new': true }
                                           )
} else {
   const { brawl, fight }: any = await userModel
                                          .findOneAndUpdate(
                                               { _id: req.userData._id },
                                               { $inc: { 'brawl.remaining': -1 } },
                                               { 'new': true }
                                          )
}

// typescript error here
// "Cannot find name brawl", and "Cannot find name fight"

console.log(brawl, fight)

not sure why typescript cannot find name brawl and fight, it could be a problem with typescript error handling in the case of if else statements,

but if script is running, no problem has occurred.

frost kazuma
  • 350
  • 1
  • 8
  • 24
  • One problem I see is that you have typed `const { brawl, fight }: any` as `any`. This is the reason typescript wont give compiletime error. If you change the type to something like `const { brawl, fight }: userModel` it will throw compile error if those properties does not exist – Sohan Jan 20 '20 at 08:58

1 Answers1

5

const and let are block scoped so they are not available in a higher scope than the block in which they are declared in. You are trying to access them outside the block they are declared in. You can declare them with let in the higher scope.

let result: any;
if (!challengeType) {
   result = await userModel.findOneAndUpdate(
           { _id: req.userData._id },
           { $inc: { 'fight.remaining': -1 } },
           { 'new': true });
} else {
   result = await userModel.findOneAndUpdate(
           { _id: req.userData._id },
           { $inc: { 'brawl.remaining': -1 } },
           { 'new': true });
}

// typescript error here
// "Cannot find name brawl", and "Cannot find name fight"
const { brawl, fight } = result;
console.log(brawl, fight);

You may have to fix some TypeScript syntax as I don't really know TypeScript, but you should get the general idea here.


Actually, you can DRY this up a bit and remove a bunch of repeated code.

const queryType: string = challengeType ? 'brawl' : 'fight';
const { brawl, fight }: any = await userModel.findOneAndUpdate(
                              { _id: req.userData._id },
                              { $inc: { [`${queryType}.remaining`]: -1 } },
                              { 'new': true });
console.log(brawl, fight);

not sure why typescript cannot find name brawl and fight

Because you're trying to access those variables outside the scope in which they are declared. The body of an if or else is a separate block scope and both let and const are only available inside that scope (note var is function scoped, not block scoped).

it could be a problem with typescript error handling in the case of if else statements,

No, it's not a typescript problem. It's how the language is designed. It is a feature of the language to limit the scope that variables can be accessed in. Javascript is the same in this regard.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • yeah but, why can i still access it outside the block scope of if else? is it because how typescript compiles it? – frost kazuma Jan 20 '20 at 08:43
  • @frostkazuma - I don't understand that last comment. FYI, I added a cleaned up version to my answer. – jfriend00 Jan 20 '20 at 08:48
  • @frostkazuma there is no *problem* - this is literally a feature of TS. If it doesn't throw a compilation error for `console.log(brawl, fight);` outside the `if/else` blocks, then you must have them defined in the outer scope. – VLAZ Jan 20 '20 at 08:48
  • 1
    it displays an error on my VS code but, it works perfectly when I run it, i do not know why, im using ts-node-dev to run my development environment. but maybe it would probably have an error if i compiled it to JS anywaysss i understood my mistake, thanks for your answer and the cleaned up version, will be using it!so stupid of me to forget blocked scoped lol – frost kazuma Jan 20 '20 at 10:31