Giter Club home page Giter Club logo

Comments (4)

RyanCavanaugh avatar RyanCavanaugh commented on April 27, 2024

Narrowing doesn't really have a way to represent "this or the other thing must be undefined" in a way that can be propagated around - it has to make a definitive determination about a particular variable.

from typescript.

dgetu avatar dgetu commented on April 27, 2024

I see. So when we have some type guard around a property access, e.g. foo.bar without optional chaining, whatever the type checker infers about bar can only be used to modify the contextual type of foo because foo is the only binding in scope?

If that's the case, I think the "either foo or bar is undefined" assertion can be reframed as "foo has type undefined | { bar: undefined }."

// I'm making the (probably false) assumption that TypeScript can recognize the law of excluded
// middle between Nullish<T> and NonNullable<T>
// `any` is a corner case, but I don't think that it needs to be handled any differently
type Nullish<T> = T & (null | undefined);
interface Foo {
  bar?: Array<number>;
}

const foo = undefined as Foo | undefined;
if (!foo?.bar) {
  type NarrowedFoo =
    // foo is nullish
    | Nullish<typeof foo>
    // xor foo.bar's type is nullish
    | { bar: Nullish<NonNullable<typeof foo>["bar"]> };
  // foo ought to have type `NarrowedFoo`, but this assignment causes a type checker error
  const narrowedFoo: NarrowedFoo = foo;
}

from typescript.

fatcerberus avatar fatcerberus commented on April 27, 2024

I think the "either foo or bar is undefined" assertion can be reframed as "foo has type undefined | { bar: undefined }."

This would require #42384. Type narrowing operations on foo.bar currently don’t have any effect on the type of foo, except for the specific case of discriminated-union narrowing.

from typescript.

typescript-bot avatar typescript-bot commented on April 27, 2024

This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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.