Giter VIP home page Giter VIP logo

Comments (6)

fatcerberus avatar fatcerberus commented on May 10, 2024

This just looks like a bug - there shouldn't be an error in this code at all.

from typescript.

Andarist avatar Andarist commented on May 10, 2024

This just looks like a bug - there shouldn't be an error in this code at all.

What would be the correct behavior here? We get no implicit any error here outside of strictNullChecks (which this issue is about):

const obj = {
  prop: null, // Object literal's property 'prop' implicitly has an 'any' type.(7018)
};

In the OP's example the computed type of bar is:

const bar: {
    c?: undefined;
    p: any;
    s: any;
} | {
    c: string;
    p: string;
    s: string;
}

I guess that p and s in the first member could somehow inherit here a string type. It would require special-casing this here and investigating time in a strictNullChecks: false cases is not something I'm that interested in doing myself :P Is it even worth it at this point? Fixing error positions here seems like a reasonable solution to me.

from typescript.

fatcerberus avatar fatcerberus commented on May 10, 2024

What would be the correct behavior here?

To be 100% clear, the behavior I'm calling a bug is there appears to be an error on the object literal returned by the function doSthWithParams; bar seems to not be an error. If either one of the returns in that function is removed, the error in question goes away, which just feels like the kind of spooky action at a distance Ryan always harps against.

from typescript.

fatcerberus avatar fatcerberus commented on May 10, 2024

Anyway, if the reported behavior isn't a bug, this surely is:

function doSthWithParams(params: unknown) {
  return {
    c: 'foo',
    p: 'bar',
    s: 'baz',
  };
}

const bar = {
  p: null,
  s: null,
  ...doSthWithParams({
    p: 'hello',
    s: 'world',
  }),
};

Variable 'bar' implicitly has an '{ c: string; p: string; s: string; }' type.

Umm... wut (also note that this error names the variable bar, while the error in the OP doesn't)

from typescript.

fatcerberus avatar fatcerberus commented on May 10, 2024

Even more janky fun:

  1. Comment out return {} in the playground from the OP
  2. Observe that bar now becomes an error (the same as the one demonstrated above)
  3. Uncomment the return
  4. All errors are now gone!

from typescript.

Andarist avatar Andarist commented on May 10, 2024

I also noticed the above and I think it's fixed with my PR. I don't think it's worth looking more into it - the problem was that the error was reported on an undefined node and that should never happen. When that happens (node === undefined) a compiler-level diagnostic gets created (with file: undefined on it). I think that getDiagnosticsWorker might accidentally be dropping them here but that it only affects a situation that shouldn't exist in the first place.

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.