2

I'm codding some library with TypeScript, and I want to protect calls my methods from incorrect meaning data. Let's see next example:


type BookId = number;
type AuthorId = number;

BooksService.getById(bookId: BookId): Book {
    // ...
};

let authorId: AuthorId;

// I need to get some type error in next lene, because getById() expect receive BookId, not AuthorId:
book = BooksService.getById(authorId);

Is it possible to get this error? How to upgrade my example to get it?

Maksim
  • 49
  • 5
  • A number is a number. How is a compiler supposed to understand the difference just by the name of a variable? – Andreas Mar 29 '19 at 12:02

0 Answers0