Hacker News new | past | comments | ask | show | jobs | submit login

Typescript taught me that I actually don’t care about types and all I care about is the shape of data. In most cases I think of types/interfaces in Typescript as strict data definitions. A function takes in a collection of data; as long as the data matches the shape I expect, don’t care what the data represents.



> Typescript taught me that I actually don’t care about types and all I care about is the shape of data.

Seconding leafario2 here, that's exactly what types do: They give guarantees about the shape of data.

Maybe what you meant was you don't care whether an object is of type Customer or Supplier, as long as it has an element/field named address. That's called "structural typing" (as opposed to "nominal typing").

Few static type systems have structural typing, probably because in practice, you actually do care whether the object is a Customer or a Supplier, even if their fields have the same names. Rust, for example, is nominally typed, but you can define a trait "HasAddress" with a trait function "fn address(&self) -> &str" and implement it for Customer and Supplier, to simulate structural typing where you need it.


I don’t know if I do care that customer is or isn’t a supplier. I have been using Nominal type systems in C++ and C# for a long time and I’ve started drifting into an architecture style that leverages interfaces as my types which I just learned is structural typing. I really liked the concept in Golang and then I really started using it in TypeScript and got hooked. It makes so many programming problems easier to implement, maintain, and understand. I now mix and match nominal and structural types in my projects, using them where they best fit.


What you use the word “type” for is a nominal type and your shapes are structural types. The utility and different trade offs between nominal and structural types were explored a lot back in the 90s.


Nominal types are on Typescript's roadmap: https://github.com/Microsoft/TypeScript/wiki/Roadmap

Side note: An Introduction to Nominal TypeScript: What are nominal types and why should I use them? : https://medium.com/better-programming/nominal-typescript-eee...


This is awesome.


Thanks, I don’t think I learned the about these concepts in college; granted, I dropped out because I am ADHD to the max. I usually adopt or adapt what I observe in others code, my old code, or idea from other fields entirely. I like to adapt and improve techniques I see and fit them best into my projects.


Everything old is new again.


That's exactly what types are for dude




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: