question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Show both narrowed type and declared type

See original GitHub issue

Suggestion

🔍 Search Terms

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

✅ Viability Checklist

My suggestion meets these guidelines:

  • 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 TypeScript’s Design Goals.

⭐ Suggestion

📃 Motivating Example

https://twitter.com/Rich_Harris/status/1437490023763415043

is roughly a confusion on narrowed vs storage type of a given field/binding

// _: any
let _;

// _: any
_ = new Date();

// _: any, but completions from Date
// type doesn't match usability, only assignability
_.getFullYear();

💻 Use Cases

Current UI / typing only shows what can be assigned to a value storage location given a source text location. Need UI to show what can be used from a value storage location given a source text location.

Much of the time these should be the same but showing something about the narrowing would apparently remove some confusions, for example (bikeshed):

let _;
_ = new Date();

// _: any
_;

// becomes...

// _: any as Date
_;

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
MartinJohnscommented, Sep 30, 2021

@justingrant I don’t see how a local variable has any relevant runtime footprint, but you can use a Type Assertion here:

return a as string;

That an unknown variable does not narrow on assignment is intentional.

1reaction
DanielRosenwassercommented, Sep 14, 2021

One technical problem (I believe) is that in

   let x;
   x = new Date()
// ^?

by the point of assignment, control flow analysis hasn’t changed the type of x - it’s only after the assignment of x that we’d ever be able to show a different type. As a user, I would expect to see Date, but for all intents and purposes, I think you’d still end up with any.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Narrowing - TypeScript
TypeScript takes this into account as a way to narrow down potential types. For example, with the code: "value" in x . where...
Read more >
Type-Safe TypeScript with Type Narrowing - Rainer Hahnekamp
This article shows common patterns to maximize TypeScript's potential for type-safe code. These techniques are all part of the same group, ...
Read more >
Type Narrowing in TypeScript - Maina Wycliffe
In this article, we are going to learn various ways you can narrow types. Type narrowing is the process of moving a type...
Read more >
Why can Typescript not figure out the type in my code?
What you're looking for here is called type narrowing, where you take a value that could possibly be more than one type and...
Read more >
narrowing types via type guards and assertion functions - 2ality
In other words: The type unknown is too general and we must narrow it. In a way, unknown is also a union type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found