beginners-typescript-tutorial
beginners-typescript-tutorial copied to clipboard
Another solution for Q14: declare precise value in interface
.....
interface User {
id: string;
firstName: string;
lastName: string;
}
interface Post {
id: User["id"];
title: string;
body: string;
}
interface Comment {
id: User["id"];
comment: string;
}
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.