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.
Moving to Rust doesn't eliminate validation altogether, but you don't have to do any type related validation which is nice.
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.`);
```
[1] https://gist.github.com/dcolthorp/aa21cf87d847ae9942106435bf...
[2] https://dev.to/gcanti/functional-design-smart-constructors-1...