Skip to content

API: getTypeAtLocation of node inside non-null assertion returns apparent type of TypeParameter with nullable constraint #21317

Closed
@ajafff

Description

@ajafff

TypeScript Version: 2.7.0-dev.20180120 (20180119 works as expected)

Search Terms:

Code
--strictNullChecks is enabled

function test<T extends string | undefined, U extends string>(param1: T, param2: U) {
    param1;
    param1!;
    param2;
    param2!;
}

Expected behavior:
checker.getTypeAtLocation(node) where node is the Identifier param1 or param2 always returns a ts.TypeParameter.

Hovering param2 in both locations shows (parameter) param2: U extends string
Hovering param1 in both locations shows (parameter) param1: T extends string | undefined

Actual behavior:
result of checker.getTypeAtLocation(node):

  • on both uses of param2 returns ts.TypeParameter
  • on the first use of param1 returns ts.TypeParameter
  • on the second use of param1 (passing the Identifier as argument, not the NonNullExpression) returns ts.UnionType (string | undefined)

Hovering param2 in both locations shows (parameter) param2: U extends string
Hovering the first use of param1 shows (parameter) param1: T extends string | undefined
Hovering the second use of param1 shows (parameter) param1: string | undefined
The result of param1! is type string which is not valid according to #20974.

This change was introduced in #20995 as a bugfix. Looking at the baseline changes this might even be intended.

IMO this is pretty inconsistent if it only applies to TypeParameters with nullable constraint.
According to the reasoning in #20974 (comment) this new behavior is invalid.
And it's a breaking change for API users.

Playground Link: Playground doesn't use the correct nightly

Related Issues:
#20995

Metadata

Metadata

Assignees

No one assigned

    Labels

    APIRelates to the public API for TypeScriptBugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions