#Why is this properties of undefined

64 messages · Page 1 of 1 (latest)

spice gale
#

circuit.ts:

import LocationFinder from './locationFinder';

export default class Circuit {
    constructor(circuitParser: any) {
        this.circuitId = circuitParser.circuitId;
        this.url = circuitParser.url;
        this.circuitName = circuitParser.circuitName;
        this.location = new LocationFinder(circuitParser.LocationFinder);
    }

    circuitId: string;
    url: string;
    circuitName: string;
    location: LocationFinder;
}

locationFinder.ts:

export default class LocationFinder {
    constructor(locationParser: any) {
        this.lat = parseFloat(locationParser.lat);
        this.long = parseFloat(locationParser.long);
        this.locality = locationParser.locality
        this.country = locationParser.country
    }
  
    lat: number;
    long: number;
    locality: string;
    country: string;
}

I am fairly new to using typescript, so I am not really familiar with this stuff. I don't quite understansd why TypeScript complains why LocationFinder is underfined (I assume that beacuse I would have gotten errors before about circuitPaser being undefined if it was). If anyone needs more information, here is the GitHub Repository: https://github.com/ultimatehecker/Formula-One-Wrapper

GitHub

A wrapper of the Ergast API for Node.js powered by TypeScript - GitHub - ultimatehecker/Formula-One-Wrapper: A wrapper of the Ergast API for Node.js powered by TypeScript

verbal socketBOT
#
Ascor8522#7606

Preview:```ts
export class Circuit {
public circuitId: string
public url: string
public circuitName: string
public location: LocationFinder

public constructor(circuitParser: any) {
this.circuitId = circuitParser.circuitId
this.url = circuitParser.url
this
...```

native quarry
#

can't reproduce

#

also, you shouldn't use any for the parameters in your code (circuitParser: any)
you're basically disabling type-checking and allowing wrong/unsafe code

spice gale
#

oh yeah I know that, ive been told that before, ill come back and fix all of it later

spice gale
native quarry
#

we usually prefer putting the properties first

spice gale
#

oh ok

native quarry
#

so that we know what properties exit before doing anything else

#

also, I added the visibility keywords (public)

#

they are "optioanl"

#

if you don't put anything, by default, the fields and methods are public

spice gale
#

yeah they make their values visible in other files i think

native quarry
#

yes, exactly

spice gale
native quarry
#

well, there is no problem in the code you shared

#

what error do you have exactly? could you show it?

spice gale
#

ah yeah thats what I thought

#

    TypeError: Cannot read properties of undefined (reading 'lat')

      1 | export default class LocationFinder {
      2 |     constructor(locationParser: any) {
    > 3 |         this.lat = parseFloat(locationParser.lat);
        |                                              ^
      4 |         this.long = parseFloat(locationParser.long);
      5 |         this.locality = locationParser.locality
      6 |         this.country = locationParser.country

      at new LocationFinder (src/config/circuits/locationFinder.ts:3:46)
      at new Circuit (src/config/circuits/circuit.ts:14:25)
      at new Race (src/config/races/races.ts:9:24)
      at src/client/races.ts:24:32
      at src/utils/request.ts:10:13```
#

oh wait thats a different error

native quarry
#

what does your tsconfig look like?

spice gale
#
{
  "compilerOptions": {
      "outDir": "./lib",
      "allowJs": true,
      "lib": ["dom", "dom.iterable", "esnext"],
      "target": "ES2022",
      "module": "esnext",
      "moduleResolution": "node",
      "esModuleInterop": true,
      "skipLibCheck": true,
      "strict": true,
      "forceConsistentCasingInFileNames": true,
      "noEmit": false,
      "noEmitHelpers": false,
      "importHelpers": true,
      "isolatedModules": true,
      "resolveJsonModule": true,
      "jsx": "preserve",
      "incremental": false,
      "paths": {
        "@/*": ["./*"]
      }
  },
  "include": ["./src/**/**/*"],
  "exclude": ["node_modules"]
}
native quarry
#

it's weird you're having this error since you defined locationParser as any

spice gale
#

yeah

#

i mean it works for now, not the best of solutions

#

should change the types laster

native quarry
spice gale
#

ok thanks

#

chnaged the tsconfig

spice gale
#

!helper

dense gulch
#

ummmmm

dense gulch
#

(cc @native quarry)

spice gale
#

yeah seems to be

dense gulch
#

giveaways:

  • happening during test
  • stack trace
  • class name ("TypeError")
#

so somewhere something is doing new LocationFinder(undefined)

eager snow
#

also 2 **/** is redundant

spice gale
#

why would that be?

eager snow
#

** can match any amount of path segments

spice gale
#

although circuitPaser does have a type

#

it may be a any type but nevertheless a type

dense gulch
#

why is the parameter any anyway

spice gale
#

i dont really know what it would be

eager snow
dense gulch
#

note that any means "turn off the type system for this value"

spice gale
#

i moved from javascript to typescript

dense gulch
#

also "don't know what it would be" is unknown

#

not that it'll help here

#

also uhhh

#

why are you just copying properties to the class

spice gale
#

ok yeah let me try finding the type

#
import LocationFinder from './locationFinder';

export default class Circuit {

    public circuitId: string
    public url: string
    public circuitName: string
    public location: LocationFinder

    constructor(circuitParser: unknown) {
        this.circuitId = circuitParser.circuitId;
        this.url = circuitParser.url;
        this.circuitName = circuitParser.circuitName;
        this.location = new LocationFinder(circuitParser.location);
    }
}

you measn this?

dense gulch
#

often people just use an interface Circuit and do let x: Circuit = { object literal here }

spice gale
#

i could make it an interface

#

never have used interfaces but ikll look at some docs

spice gale
# dense gulch often people just use an `interface Circuit` and do `let x: Circuit = { object l...

like this: ```ts
import LocationFinder from './locationFinder';

export interface circuitParser {
circuitId: string
url: string
circuitName: string
location: LocationFinder
}

export default class Circuit {
constructor(circuitParser: circuitParser) {
let circuitId = circuitParser.circuitId;
let url = circuitParser.url;
let circuitName = circuitParser.circuitName;
let location = circuitParser.location;
}
}

dense gulch
#

oh no

#

no i mean

let circuit: Circuit = {
  circuitId: 1,
  url: 'example.com'
  // + the rest
}
spice gale
#

oh ok