Closed as not planned
Description
π Search Terms
- asserts
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about this
β― Playground Link
π» Code
function string(value: unknown): asserts value is string {
if (typeof value != "string") {
throw new TypeError(`Expected a string`)
}
}
const works = (source: unknown) => {
string(source)
return source.toUpperCase()
}
const breaks = (source: unknown) => {
console.log(string(source))
return source.toUpperCase()
}
const alsobreaks = (source: unknown) => [
string(source),
source.toUpperCase()
]```
### π Actual behavior
Functions `break` and `alsobreak` do not type check and produce following error: "'source' is of type 'unknown'.", because type refinement produced by function "string" don't seem to apply, if asserting function is not a statement.
### π Expected behavior
I would expect all three functions to type check, because `string` type refinement should apply to even if refinement is nested in an expression.
### Additional information about the issue
Provided example seems contrived because it was reduced to bare minimum, however there are legitimate uses for refinements like the above [for example](https://www.typescriptlang.org/play/?#code/MYGwhgzhAEBqYCcA8AVaBeaBXAdgaxwHsB3HAPmgG8AoaaAFwE8AHAUwC5ocsQQAfCPQQBLHAHNa0YIRyCEWYPUIIAFAEoqkuvQAWwiADombDFx4hJAX0kBbMPWA6VANzAgsHbPiKk1nV+6s0PrQaDR0dMIAZtAqxqyEMQEe0ACE6Ji6+kYsrBrhEdo6CCRcrMShuQCiCCWqAAbwgdAAJJTJrJbQACaErDBE9FIyUcIINgyEDLmtlFmG8Zb1alrQXawgEEEFhQis9FgIOAzyrKvWdNbW1PGhrOOoprgEJOSmaHxwiKhk1NSgkBgAEkcEMCoJ7MJgNA7A4nPR7jZOChEX5oID7vQYAjxsEYCiHtwbAAje4UHbBGJxXKJBiItIZaAAciJpIQTOgADJOdAAHJYEn3Az6EEIsT3OKo-KrCK6EoVHDlSpsGp1FRMqoADzYilY3XRZkF7JWhQua2o1wBUGgAAVCKIwZIIfQoTD7I4VBBCIdgJ5KNBNZx4MhWWSADTQRhB76hhAUSzSwoAeiT0AM6asFr+KegAEEoJiYMRlHh0cAStbnawbKxQRB-jJBAHTIqKsHqKKDLCPZqVnaHV33U5-ZqI1Hmwm-tQcwARPoDQhDYsIUuiaCsbV7KDCRsN2RDRgtpXt6SyQggVgGECEMQqSSd7tORhqMOSfugwdwlQjziMMe-tYVhWPcm1POQwAdGBMAAbXvD9HxUZ9Xzod96E-D0f0jf9I0A6gAF0-iiXBFB3Y5HAgnBPW9BBfU4Z4fBwRNoD2A4jikHQKKzEChgALyPNtEBA89L2vW8HyHFQeLUPt7XgiTMJ47C+ITIA) when composing set of constraints for later execution.