Skip to content
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

unknown should be narrowable to {} #32347

Closed
evmar opened this issue Jul 11, 2019 · 2 comments · Fixed by #49119
Closed

unknown should be narrowable to {} #32347

evmar opened this issue Jul 11, 2019 · 2 comments · Fixed by #49119
Labels
Fix Available A PR has been opened for this issue In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@evmar
Copy link
Contributor

evmar commented Jul 11, 2019

TypeScript Version: 3.6.0-dev.20190711

Search Terms: unknown narrow toString hasOwnProperty

Code

function f1(o: unknown) {
  if (o) {
    o.toString();
  }
}
function f2(o: {}|null|undefined) {
  if (o) {
    o.toString();
  }
}

Expected behavior:
Both succeed.

From user perspective I believe unknown behaves like an alias for the second type. So another way of looking at this problem is that given f1's signature, it's hard to explain why to someone to need to switch it to f2's signature based on whether the body needs narrowing.

Actual behavior:
f1 invalid on the toString, Object is of type 'unknown'.

Playground Link:
http://www.typescriptlang.org/play/#code/GYVwdgxgLglg9mABMAjACjgLkeA1mOAdzAEpEBvAKEURmEQzKpprgDoo4BlKAJxjABzNCQDc1RAF9K00JFgJkAJgzZykgD5gQAGx0bwAEwCmwAccNMJdBnCstE7Tj35CR4mtMlA

Related Issues:
In this comment
#28131 (comment)
@ahejlsberg says it's a limitation of the CFA.

This bug talks about other approaches to narrowing unknown:
#25720
and in particular @andy-ms points out a bug that feels similar:
#25720 (comment)

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jul 31, 2019
@taralx
Copy link

taralx commented Apr 5, 2022

Also running into this when dealing with JSON, as the result of unmarshaling is reasonably unknown, and result ? result.toString() : "" doesn't type check...

@DanielRosenwasser
Copy link
Member

PR is out at #49119

@DanielRosenwasser DanielRosenwasser added this to the TypeScript 4.8.0 milestone May 18, 2022
@DanielRosenwasser DanielRosenwasser added In Discussion Not yet reached consensus and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels May 18, 2022
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix Available A PR has been opened for this issue In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants