Coder Social home page Coder Social logo

Comments (6)

RyanCavanaugh avatar RyanCavanaugh commented on May 1, 2024

Not really sure how to reason about this one.

On the one hand, TS assumes that if you write code, you did so for a reason. So if there's a default case, then it must be reachable via some means that you know about but we don't, so it's fair game for computing the return type. You wouldn't be allowed to return "foo"; here, for example, and the implicit return undefined; that exists at the implied exit point of the switch exists by the same logic.

On the other hand, in the absence of the default, we know the switch is exhaustive, so any code in the default block shouldn't need a return.

But again, you wrote the default block for a reason, and didn't throw out of it, so if that code isn't 100% dead, it ought to be an error. Otherwise you'll get an unsound undefined later in your program in a way that's really hard to diagnose.

I would just delete, or return or throw here for clarity. The current behavior doesn't seem wrong enough justify endangering people who do have an actually-reachable default (as is somewhat common in the case of e.g. side effects or array access)

from typescript.

Mng12345 avatar Mng12345 commented on May 1, 2024

The default case here is designed to allow the TypeScript compiler (tsc) to automatically detect when a new variant of the Size type is introduced. If the size in the default branch is of the new variant instead of never, it will trigger an error, reminding developers that a case has been missed.

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on May 1, 2024

You don't need one, though? https://www.typescriptlang.org/play?target=1#code/C4TwDgpgBAyglgL2gXigcgDYEMBOBzCNKAH3QDsB7HAWywyNLQGdaN6T1q4y40AoAMYUyTYFCaIUUAN4BfKFiaxJg4aKgAjDBQEBrKKgA8AJQA0APgAUAMwBcUSwEoD5qMefJX0vlF9QcEMAArjhkUNZOfLJ8qiJiAkxKqFo6upZO9mRB1BoQOC4yPn5MAO5wwAIAFg4SSM7efo1QAorQaJQ0dGj2DU19AcGhUACMRX3Rfb4tTG3Y+IQ9Y5O+AyFhAExLjROT021cPN2Fy42rQwC0w1Bbfju+0bKOQA

from typescript.

Mng12345 avatar Mng12345 commented on May 1, 2024

Yes, I can use the return type of the function as a workaround to perform exhaustive checks on the switch statement.

from typescript.

Mng12345 avatar Mng12345 commented on May 1, 2024

For absolute correctness, currently, the only way is to wrap the switch statement within a block function to ensure that no case is missed. Although typescript-eslint can also perform such checks, it's not always easy to configure typescript-eslint properly in every project.

from typescript.

Mng12345 avatar Mng12345 commented on May 1, 2024

It would be great if tsc can add an options config for switch exhausiveness check.

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.