Show both narrowed type and declared type
See original GitHub issueSuggestion
🔍 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:
- Created 2 years ago
- Reactions:5
- Comments:8 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@justingrant I don’t see how a local variable has any relevant runtime footprint, but you can use a Type Assertion here:
That an
unknown
variable does not narrow on assignment is intentional.One technical problem (I believe) is that in
by the point of assignment, control flow analysis hasn’t changed the type of
x
- it’s only after the assignment ofx
that we’d ever be able to show a different type. As a user, I would expect to seeDate
, but for all intents and purposes, I think you’d still end up withany
.