Skip to content

non-null assertion not working on type parameter with constraint #20974

Closed
@ajafff

Description

@ajafff

TypeScript Version: 2.7.0-dev.20171230

Code

function takeString(p: string) {}

function foo<T extends string | undefined>(param: T) {
  takeString(param!); // error on this line
}

Expected behavior:
Because of the constraint the compiler knows the type of param can only be string | undefined. Asserting the type with a non-null assertion should result in string and the code should compile without error.

If this is working as intended, I'll write a lint rule to warn about non-null assertions on type parameters.

Actual behavior:

Argument of type 'T' is not assignable to parameter of type 'string'.
  Type 'string | undefined' is not assignable to type 'string'.
    Type 'undefined' is not assignable to type 'string'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions