Re:Tricks I wish I knew when I learned TypeScript
by pietrovismara on Tuesday October 12, 2021 @08:23AM
(#28838816)
You still can extend types with type intersections:
type A = { id: number }
type B = A & { name: string }
const b: B = { id: 0, name: 'foo' }
Re:Tricks I wish I knew when I learned TypeScript
by moron4hire on Tuesday October 12, 2021 @09:58AM
(#28839720)
That's creating a new type B. In contrast, interfaces can have their definition spread across multiple code units.
interface X {
x(): void;
}
interface X {
y(): void;
}
class Y implements X {
x(): void {
console.log("hello");
}
y(): void {
console.log("world");
}
}
const z = new Y();
z.x();
z.y();
This is important for keeping up with API changes in browsers that may happen faster than the DefinitelyTyped project can keep up.
“For the most part, you can choose based on personal preference, and TypeScript will tell you if it needs something to be the other kind of declaration. If you would like a heuristic, use interface until you need to use features from type.” https://www.typescriptlang.org/docs/handbook/2/everyday-type...