IceGuye Blog

People Just Hate Type Safety in Programming

I just realize how many people just hate type safety, and how much effort they can spend to just break the type safety.

So the story is that, because their TypeScript APIs have nearly 0 documentation, so I have to dive into their TypeScript codes to implement their features in Rust. Then I found something funny in their TypeScript.

So they define an interface, just like a struct in Rust.

Car {
  engine: string,
  horsepower: number,
  fuel_type: string
}

my_car = Car {
  engine: "Cummings",
  horsepower: 350,
  fuel_type: "Diesel"
}

Then they define another interface, totally different from the Car. Let's say, it is a Cow:

Cow {
  breed: string,
  age: number,
  what_to_eat: string
}

Now they have a function, which promises to return a Cow, not a Car. But for some reasons, they only have a Car. So what they do is like that:

return my_car as Cow;

In TypeScript, the "as" will not convert the data type. It just allows you to break the promising. After they compiling into original JavaScript, you will see they actually do nothing.

Now you get a cow that has a Cummings diesel engine with 350 housepower... while all other cows are normal...

... and they really like to do this...




Back to Blog's index