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

asserts keyword with intersection type is not working #56256

Closed
samchon opened this issue Oct 30, 2023 · 4 comments
Closed

asserts keyword with intersection type is not working #56256

samchon opened this issue Oct 30, 2023 · 4 comments

Comments

@samchon
Copy link

samchon commented Oct 30, 2023

πŸ”Ž Search Terms

  • asserts
  • intersection

πŸ•— Version & Regression Information

Occurs since v3.7 update, the first update supporting the asserts statement.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBAMwK4DsDGMCWEVwIYDOBwsAyjFJigOYAUVYSMAXHKgNYoQDuKAlK0LFYBOAyZjRBClWpwAZHGmUacAN4AoONrEI4tGAE8wwCHvHwAhAF5rcAOTLZ9vlp3uYACyg84KYNxwAKJQPlC09gCSKIzwmKJc8HhKMjQuANxu2lDAMEhQuBaZAL5AA

πŸ’» Code

export function assertString(input: unknown): asserts input is string & string {
    if (typeof input !== 'string')
        throw new Error('Input is not a string');
    return input;
}

πŸ™ Actual behavior

It is possible to declare an asserts guard function with intersection return type. There's no error until that.

However, the intersection return type does not work, and just working as a void return type.

Type 'string' is not assignable to type 'void'.

πŸ™‚ Expected behavior

Hope the intersection return type to work.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

MartinJohns commented Oct 30, 2023

The type string & string is the same as string.

It's not clear what you actually want and how it should work. Assertion functions don't return anything. You're just declaring that whatever input is is of the type string & string (which is equal to string).

If I had to guess I'd say you want #40562, and just mistakingly throw unrelated terms together (the intersection type).

Regardless, the error is correct. There is no bug here.

@samchon
Copy link
Author

samchon commented Oct 30, 2023

Got it, what I want is to making a function returning T value. Exact type maybe (asserts input is T) & T.

@MartinJohns
Copy link
Contributor

That would be a feature request, not a bug report, and it already exists: #40562

Currently such functions are not possible.

@samchon
Copy link
Author

samchon commented Oct 30, 2023

Thanks for fast response. I should consider another way.

@samchon samchon closed this as completed Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants