You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{strictasassert}from'assert';asyncfunctionoverwrite(err: unknown){err='hello';assert(err.startsWith('h'));// err is 'unknown' but should be 'string'}
π Actual behavior
Errors because err is type unknown.
π Expected behavior
No error because err is a string by that point.
Motivating example
I am reading off .error from an Angular HttpErrorResponse object. error is any, and can be at least string, JSON or Blob. I set it to "unknown" for type safety.
asyncfunctiondealWith(err: unknown){if(errinstanceofBlob){constisJson=err.type.endsWith('json');err=awaiterr.text();if(isJson){err=JSON.parse(err);}}// code that checks whether err is string or JSON and deals with it}
The text was updated successfully, but these errors were encountered:
I note that the most general case #36579 has been marked as "Working as Intended". Most of the other tickets are still in discussion mode. Is narrowing unknown types on assignment a possibility? I'm not really concerned about other types: for the most part, they're much easier to work with.
Bug Report
π Search Terms
unknown, overwrite, replace
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Errors because
err
is type unknown.π Expected behavior
No error because
err
is a string by that point.Motivating example
I am reading off
.error
from an AngularHttpErrorResponse
object.error
isany
, and can be at least string, JSON or Blob. I set it to "unknown" for type safety.The text was updated successfully, but these errors were encountered: