Skip to content

Support preserving type narrowing inside closures in some simple cases #55528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
fabiospampinato opened this issue Aug 27, 2023 · 3 comments Β· Fixed by #56908
Closed
5 tasks done

Support preserving type narrowing inside closures in some simple cases #55528

fabiospampinato opened this issue Aug 27, 2023 · 3 comments Β· Fixed by #56908
Labels
Duplicate An existing issue was already created

Comments

@fabiospampinato
Copy link

fabiospampinato commented Aug 27, 2023

πŸ” Search Terms

  • narrowing
  • type
  • closure
  • function

βœ… Viability Checklist

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ Suggestion

I understand that types are not kept narrowed inside callbacks because the narrowing could in general not hold anymore in there, however there are some "simple" scenarios where we can be sure that the narrowing holds, for example:

function foo () {

  let maybe: string | null;

  maybe = 'foo';

  console.log ( () => {

    maybe;
    // ^?

  });  

}

This scenario is a sort of a simple edge case of the general problem because:

  • The maybe variable holds a primitive value, so it will be passed to other functions by copy, so those functions outside the closure can't change its value inside the closure.
  • The code executed before the console.log call is done forever, since it doesn't have any callbacks that assign to maybe, and it's not wrapped in an unterminated loop.
  • The "code" after the console.log call also has no assignments to "maybe", in fact it has no references to "maybe" at all.

Under these conditions it's impossible for narrowing the type inside our function in the example above to be wrong, so it should be preserved.

πŸ“ƒ Motivating Example

Playground: https://www.typescriptlang.org/play?#code/FAMwrgdgxgLglgewgAhAhyAUBKZBvYYZZAGwFMZkBbAQwE8AjMgLmQGcYAnOCAc2QA+yCGBIkA3IWK1GZZAF5kAcjQIlkosihI2CcgDoSCfpiy55APnxTi1ek0m3kAemfIAegH4bAX2zjiQh9gIA

πŸ’» Use Cases

Preserving narrowing in simple cases like the one mentioned above is very handy and allows the user to write clean code in more scenarios.

@fabiospampinato fabiospampinato changed the title Support narrowing type inside closure in some simple cases Support preserving type narrowing inside closures in some simple cases Aug 27, 2023
@MartinJohns
Copy link
Contributor

I'm fairly sure this was discussed as part of #9998 already.

@fabiospampinato
Copy link
Author

Possibly, in some of those 450 comments πŸ˜… That sounds like a general umbrella issue though, maybe we need a smaller issue for this one "easily" addressable part of the problem.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Aug 28, 2023
@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants