beginners-typescript-tutorial icon indicating copy to clipboard operation
beginners-typescript-tutorial copied to clipboard

Another solution for Q14: declare precise value in interface

Open zmzlois opened this issue 2 years ago • 1 comments

.....

interface User {
  id: string;
  firstName: string;
  lastName: string;
}

interface Post {
  id: User["id"];
  title: string;
  body: string;
}

interface Comment {
  id: User["id"];
  comment: string;
}

zmzlois avatar Dec 27 '22 00:12 zmzlois

If there were a thousand interfaces with the id field, and one day you wanted to change only the id type of User to number, you would then have to change id: User["id"] in each interface. So compared to the given solution, the code would be less maintainable.

Korazza avatar Mar 29 '23 10:03 Korazza