4 ms·
That's correct. You have to insert validation code at all the entry points, which was the case for me. Moving to Rust doesn't eliminate validation altogether,
by nicolodavis 6y ago
That's correct. You have to insert validation code at all the entry points, which was the case for me.
Moving to Rust doesn't eliminate validation altogether, but you don't have to do any type related validation which is nice.
- mcny 6y agoNot sure if this is a place to ask this but if someone does not have experience working with Javascript, they might have trouble reasoning about this code: https://codesandbox.io/s/is849 https://codesandbox.io/s/is849 edit: complete code ```typescript class Person { id: number; name: string; yearOfBirth: number; constructor(id: number, name: string, yearOfBirth: number) { this.id = id; this.name = name; if (yearOfBirth < 1900 || yearOfBirth > 2020) { throw new Error("I don't understand you. Go back to your time machine."); } else { this.yearOfBirth = yearOfBirth; } } getAge(): number { const currentDate: number = new Date().getUTCFullYear(); return currentDate - this.yearOfBirth; } } class Dog { id: number; name: string; yearOfBirth: number; constructor(id: number, name: string, yearOfBirth: number) { this.id = id; this.name = name; if (yearOfBirth < 1947 || yearOfBirth > 2020) { throw new Error("I don't understand you. Go back to your time machine."); } else { this.yearOfBirth = yearOfBirth; } } getAge(): number { const currentDate: number = new Date().getUTCFullYear(); return (currentDate - this.yearOfBirth) * 7; } } const buzz: Person = new Person(1, `Buzz`, 1987); const airbud: Dog = buzz; console.log(`Buzz is ${buzz.getAge()} years old.`); console.log(`Airbud is ${airbud.getAge()} years old in human years.`); ```
- dfee 6y agoWhat’s confusing? I must’ve missed it in my cursory scan.
- s-lambert 6y agoDog and Person are structurally the same so you can assign a person to a dog and vice versa. But that's just how structural typing works and as a user of TypeScript I haven't run into a case where this'd be an issue.
- dfee 6y agoHaha oh. Yeah, assumed that the Dog definition wasn’t worthless. Indeed TS is structurally typed. And that’s nice!
- cheerlessbog 6y agoI worked on a web based editor. A library would give us a range to highlight, in 1-based coordinates. The editor control was 0-based. As you can imagine it was easy to forget to translate back and forth in one path or another. In a strongly typed language I would simply define two Range types and the compiler would eliminate the mistake. I assumed Typescript could help me in the same way but it allowed the two types to be interchanged silently because they had the same structure. Perhaps I was holding it wrong?
- panzerklein 6y agoTypescript has two hacks that help with mixing of similar data and introduce somewhat-nominal typing - branding and flavoring [1]. Also see smart constructors [2] for more functional approach. [1] https://gist.github.com/dcolthorp/aa21cf87d847ae9942106435bf47565d https://gist.github.com/dcolthorp/aa21cf87d847ae9942106435bf... [2] https://dev.to/gcanti/functional-design-smart-constructors-14nb https://dev.to/gcanti/functional-design-smart-constructors-1...
- hombre_fatal 6y agoThat's the most they could simplify the code to make the point?
- tln 6y agoA person value assigned to a dog variable
- judge2020 6y agoUnfortunately HN doesn't support commonmark's triple backtick code blocks. You'll need to use 4 spaces before each line of code.
- Stratoscope 6y agoTwo spaces. Four will work, of course, it just wastes a bit of horizontal screen space.