#TypeScript Question

1 messages ยท Page 1 of 1 (latest)

sturdy jay
#

I'm new to typescript, so I'm sorry if this is a really stupid/obvious question. If I'm creating classes for my app, do I (should I) also have types describing these classes? Or is typing in a types.d.ts file only for data that you won't be writing a class for?

#

A bit deeper:
I have a Game class (singleton). It will have a Map singleton, a Trainer singleton, and a list of MapNodes. Should I create a Game, Map, Trainer, and MapNode class? Do I also need types for each of these? Or does defining the class and instantiating each singleton do that for me?

#

It may also be redundant to have my own Map class, considering that I'm using Leaflet which comes with it's own Map class, and I'll only have one Leaflet Map. I could probably just do Game.map = L.map(...), correct?

vast cloak
#

Um if you define a class you'll have to import the class to work with it in a different file. If you define an interface in your types file, then you don't have to import the type in ecah different file to work with it. So you don't need to also create types/interfaces if you're ok importing it each time. that's probably what I would do

#

Curious what @verbal imp and @hard hound think ๐Ÿ‘€

cyan bone
#

Following ๐Ÿ‘€

hard hound
#

Hello ๐Ÿ˜„

#

So, first here is a write up on "classes" in typescript as the have some extra tools you can use vs standard JS classes
https://www.typescriptlang.org/docs/handbook/classes.html

Like James said if you add you types/interfaces to a .d.ts file. You should be able to add one of these files anywhere in you src dir or most framworks have an in-house one like next next-env.d.ts. I have been trying to do this more but lean for being more explicit and have on for each class like game.d.ts and store all delicions types in there.

quick side note: If exporting form a non d.ts file to export as a type:

export { type GameType }

That said, bases on you question I think this artical may help: https://www.sitepen.com/blog/advanced-typescript-concepts-classes-and-types#:~:text=To solve this problem%2C we can use an interface to define the constructor arguments and export that alongside the class.

class Person {
  name: string
  age: number
}

const me: Person = {
  name: 'joe',
  age: 2,
}

// OR

const you = new Person()
#

admittedly I don't use classes much lately

hard hound
#

and as far as this being a stupid question is concerned:

#

or as Dear Abby puts it:
"There is no such thing as a stupid question if it's sincere. Better to ask and risk appearing stupid than to continue on your ignorant way and make a stupid mistake."
๐Ÿ˜‚ ๐Ÿคฃ

sturdy jay
#

I feel like I'm doing svelte typescript wrong. I have (had) a types.d.ts file but my typescript hinting wasn't working unless I explicitly imported using import type { whatever } from '$lib/types/types.d'. I'm going to go spend the day on some svelte typescript tuts - if you have any recommendations, I'm all ears.

hard hound
#

@sturdy jay what does you tsconfig.json look like.

Check the "include" section. If there is not one, which would be odd (I also do not use Svelte ๐Ÿ˜ฒ ), you can add it after (outside of) your "compilerOptions" and then add types.d.ts to the deps array.

There is also a "types": ["FILE_NAME"], option in compilerOptions

poking around I see some svelte.d.ts files in peoples src dir

Those are a few ideas

  "incremental": true,
    "baseUrl": "."
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]