Skip to content

non-null assertion on TypeParameter changes string | undefined to String #21360

Closed

Description

TypeScript Version: 2.7.0-dev.20180123

Search Terms:

Code

declare function foo(p: string): void;

function fn<T extends string | undefined, U extends string>(one: T,two: U) {
    let three = Boolean() ? one : two;
    foo(one!);
    foo(two!);
    foo(three!); // error on this line
}

Expected behavior:
No type error.

Actual behavior:

Argument of type 'string | String' is not assignable to parameter of type 'string'.
  Type 'String' is not assignable to type 'string'.
    'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.

This is caused by #20995. To be fair, without that change the above was not valid at all.

Playground Link:

Related Issues:
Maybe related to #21317

Activity

  1. ahejlsberg commented on Jan 24, 2018

    @ahejlsberg
    Member
  2. weswigham commented on Jan 24, 2018

    @weswigham
    Member

    getApparentTypeForLocation (which is how we enabled this, similarly to how we've made guards on this.foo.bar work when this.foo is an indexed access type) uses getAparrentType. That replaces primitives with their corresponding interfaces; I don't think we (ever) actually want do do that here (it just didn't matter when it was only ever used for the left hand side of a property access), so it's probably appropriate to factor out the constraint-mapping behaviors and perform only that here.

  3. locked and limited conversation to collaborators on Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA 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