News for hackers. Stuff that matters.
Re:Tricks I wish I knew when I learned TypeScript by kkirsche on Tuesday October 12, 2021 @07:56AM (#28838646)
“Type aliases and interfaces are very similar, and in many cases you can choose between them freely. Almost all features of an interface are available in type, the key distinction is that a type cannot be re-opened to add new properties vs an interface which is always extendable.” …

“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...

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.