Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using class as type in TypeScript

Why can I use class as type in TypeScript, like word: Word in the code below?

class Dict {
  private words: Words = {};
// I wonder about this line
  add(word: Word) {
    if (!this.words[word.term]) {
      this.words[word.term] = word.def;
    }
  }
}

class Word {
  constructor(term: string, def: string) {}
}

I wanted to understand it through official document but couldn’t find related content.
https://www.typescriptlang.org/docs/handbook/2/everyday-types.html

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do this in most statically typed languages. By annotating word with Word you can access its class properties.

More info:
https://www.tutorialsteacher.com/typescript/typescript-class

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading