Code Monkey home page Code Monkey logo

Comments (2)

dragomirtitian avatar dragomirtitian commented on September 24, 2024

I opened #58637. This PR ensures we give errors for function declaration/class methods as well. It also only errors if the type doesn't syntactically contain undefined, and if the type can't be proven to not contain undefined. This means that number[] or T | undefined will not error, since for the first we know it does not contain undefined (so we could add it) and for the second we know it does. The only case that will error is where the type of the parameter is a type reference such as T for which we would need to lookup type information to determine if it contains undefined or not.

There is an argument to be made that this need not be an error at all. If we always add | undefined one of two things can happen: either T already contains undefined and thus the extra union is redundant but semantically equivalent, or T does not contain undefined in which case it should have been added. This does create a situation where if we align emit, TSC will seem silly. The bugs write themselves: Why is TS not smart enough to realize that T already has undefined in it and is adding this redundant union to my parameter type.

I would be happy if we were to not need an error here. @weswigham do you think adding | undefined to the type regardless would be a big source of bugs with resolution: "we did this intentionally"?

from typescript.

weswigham avatar weswigham commented on September 24, 2024

Obviously it's semantically equivalent to add an | undefined onto a type that already has an undefined, so it'd do nothing. But at the same time, we don't have a good reason to regress our emit and blindly add | undefined in all cases. It should certainly be fine for an emitter to do so, and should simplify things for it greatly, but we don't have a reason to do so in the general case. transpileDeclaration might need to, since it has to handle non-resolving type nodes that it can't determine the undefined-ness of, but I can't really see a compelling reason to adjust normal declaration emit output. It'd just produce stuff like Alias | undefined even when type Alias = Foo | undefined, which just.... isn't gonna feel great.

So I can see adjusting the fallback behavior for non-resolving types to be adding | undefined to the existing node - that'll probably help transpileDeclaration. I'm just less sure about changing the behavior more broadly. We've said a couple times that while we're happy to take improvements to declaration emit that happen to cause alignment (preserving original intent more by copying more nodes as originally written being the big one), we're not really interested in making our normal output match 1:1 with what something limited could produce just for the sake of it.

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.