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

assert type refinements don't work in expressions #60841

Closed
Gozala opened this issue Dec 22, 2024 · 4 comments
Closed

assert type refinements don't work in expressions #60841

Gozala opened this issue Dec 22, 2024 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@Gozala
Copy link

Gozala commented Dec 22, 2024

πŸ”Ž 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

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABAZygJxmA5gCgG4CGANiAKYBci4A1mHAO5gCUlByypaUyihJpiGD1QZsiAN4AoRDMHBEOKAE8ADqTjy+ZRAEIAvIgBEIzFkNMJ02dagALNA0RhS9RABVVpAKJoHaHAAGXgAeatCkACaIBCjopgFMVjIAvpKpkhAIqIj0cGjUPAY4yHAgaBAUVGC0DMyIegB8ltYm2MWl5aSJ1mikUGVIJWUVAHRQcACqKmpoAMJspDiJ6Zlg2QBGvQQF9QpDnZQ0dIwWjc2yqyVEpCNEcLitDx0VTN2yvf1og88341Mz8w4SzSkgyWSg0SIJU2pG2hT2P0O1WOdTOAG0krFRE9hl0ADSY-ajP7TTiAxaJAC6QA

πŸ’» 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.
@MartinJohns
Copy link
Contributor

This is working as intended. See #32695.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 2, 2025
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 5, 2025
@Gozala
Copy link
Author

Gozala commented Jan 13, 2025

This is working as intended. See #32695.

@MartinJohns mind clarifying a bit what the intended behavior is ? I do see liked PR says

the call occurs as a top-level expression statement, and

But then again in works function assertion works as I'd expect even though it's not a top level expression. It could be that behavior matches what PR intended, but I would argue it is all but intuitive, if string function throws on non-string wrapping the call in console.log or array should have not change anything, yet currently it does. So perhaps that was a flaw in intended behavior ?

@RyanCavanaugh Mind point what is this issue duplicate of so I can subscribe to updates there ? Thanks

@MartinJohns
Copy link
Contributor

But then again in works function assertion works as I'd expect even though it's not a top level expression.

But it is a top-level expression:

string(source);

It's not a nested expression, like in breaks and alsobreaks.

Mind point what is this issue duplicate of so I can subscribe to updates there ?

The issue is #32695, which describes that this is as intended. Another related issue would be #53195.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants