Deep JavaScript Foundations, v3

Validating Operand Types

Kyle Simpson

Kyle Simpson

You Don't Know JS
Deep JavaScript Foundations, v3

Check out a free preview of the full Deep JavaScript Foundations, v3 course

The "Validating Operand Types" Lesson is part of the full, Deep JavaScript Foundations, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle makes the assertion that the validation of operand types is a useful aspect of static typing.


Transcript from the "Validating Operand Types" Lesson

>> Kyle Simpson: One thing that I think is undervalued within TypeScript this is actually where many more of my bugs have come from. It's that in addition to being able to a static typing information about variable assignments, it can also tell us that certain operations would be invalid. In this particular case, it's saying you can't subtract a string from a number.

Because that particular preference is saying don't allow that coercion, and in this particular case, that would be a really useful help for me. I would really like for a linter to tell me, don't try to subtract a string from a number. What I'd really like is a linter that let me have that, but not let me have all the complaints about all my static type assignments.

That would be my preference. But this is, I think, an undervalued part of what they do. Is that they are actually allowing us to check the operations that we're doing where most of our business logic is, and making sure that those operations are valid. It would be nice if TypeScript would have some mechanism by which you could allow some more coercion to occur.

Because there are plenty of places, as I've already made the case, where I'd like to be able to do coercion and other Places I'd like to avoid it. It appears to me in my research that Typescript is kind of all or nothing. You opt into it or you don't opt into it.

I'd prefer there to be more nuance and configuration.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now