Skip to content

asserts keyword with intersection type is not workingΒ #56256

Closed
@samchon

Description

@samchon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions