0

I have a Typescript project in which I am reassigning variables depending on the value that the object contains.

What I am trying to do is define a variable from a property of the object, instead, if this property does not exist, a default value is established.

This is my Object:

interface InsParams {
  host?: string,
  country?: string,
  pageLimit?: number,
  pageOffset?: number,
  pageToken?: string
}

let dataObj: InsParams;

This is the variable I'm creating:

let limit: number = dataObj.pageLimit ? dataObj.pageLimit : 1000

This is the error it shows:

Error: Cannot read properties of undefined

My problem: I need if the property does not exist to assign another value to the limit variable, but it shows an error.

stark
  • 59
  • 1
  • 7
  • 1
    The issue isn't the property. The issue is that you haven't assigned **anything** to `dataObj`, so it has the value `undefined`, and you can't access any property on `undefined`. – T.J. Crowder Feb 08 '22 at 10:41
  • Separately, `dataObj.pageLimit ? ` will test if `pageLimit` is [*falsy*](https://developer.mozilla.org/en-US/docs/Glossary/Falsy), not whether it's not present. `0` is a falsy value, so if `0` is a potentially-valid value for `pageLimit`, that check would be incorrect. Instead you'd want `"pageLimit" in dataObj ?`. – T.J. Crowder Feb 08 '22 at 10:43
  • @T.J.Crowder The object can come empty but I have to declare the variables, in case that property comes empty or does not exist I want to assign a default value to it, to avoid errors in the rest of the process – stark Feb 08 '22 at 10:44
  • Does this answer your question? [Null-safe property access (and conditional assignment) in ES6/2015](https://stackoverflow.com/questions/32139078/null-safe-property-access-and-conditional-assignment-in-es6-2015) – pilchard Feb 08 '22 at 10:45
  • 1
    This runtime error could have been prevented by enabling the [`strictNullChecks`](https://www.typescriptlang.org/tsconfig#strictNullChecks) compiler option and fixing the resulting compile errors. – JSON Derulo Feb 08 '22 at 10:57

2 Answers2

3

Your issue comes from dataObj being potentially undefined. In this case, you can use optional chaining + null coalescing operator:

let limit = dataObj?.pageLimit ?? 1000;
Terry
  • 63,248
  • 15
  • 96
  • 118
  • 1
    @T.J.Crowder I can't seen to find the right issue: please close as duplicate if you can find it, thanks! – Terry Feb 08 '22 at 10:44
  • The type of the `dataObj` variable should be updated to `let dataObj: InsParams | undefined;`, so the compiler reports the original issue up front. – JSON Derulo Feb 08 '22 at 10:45
  • @JSONDerulo - For me, [it does](https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgJIgM4AU5TgWw2QG8BYAKGWQAsB7DMAfgC5kGpQBzAGgqoVoBXcFACeLNmA4gefZAAc4nCABlg+YE1YhB+AEbRelBUogB5GDAwQtyHfsNzFygCq0A1hBAT2XCgF8KCgAbG2QAEzgwODM9ACtWdGxcAgwAbiDyULBkYPVNbV0DKGQAXgiomPiAOmdVfJzGCujYuNrTNQ0c1gBGAAYBoA). – T.J. Crowder Feb 08 '22 at 10:47
  • I can't find a clean one! Nothing as clean as this. So there we are. :-) (I mean, it's probably *there*, but if neither of us can find it...) – T.J. Crowder Feb 08 '22 at 10:47
  • 1
    @T.J.Crowder you are right, it's because of the `strictNullChecks` compiler option, which the OP seems to miss. – JSON Derulo Feb 08 '22 at 10:50
  • @T.J.Crowder I had not seen this way of accessing the data to check that it is undefined, it works well and I do not see a better way to assign empty property variables – stark Feb 08 '22 at 10:57
0

Here is the solution:

let limit: number = dataObj?.pageLimit ? dataObj?.pageLimit : 1000;
  • This solution was posted several minutes ago (but with explanation). Please be sure not to accidentally duplicate answers that are already present. Also, rather than posting just code, please take the time to *explain* your answer -- what you did, and why you had to do it. – T.J. Crowder Feb 08 '22 at 10:49