Skip to content

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

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

Closed
ajafff opened this issue Jan 23, 2018 · 2 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@ajafff
Copy link
Contributor

ajafff commented Jan 23, 2018

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

@ahejlsberg
Copy link
Member

@weswigham

@weswigham
Copy link
Member

weswigham commented Jan 24, 2018

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.

@weswigham weswigham assigned weswigham and unassigned sandersn Jan 24, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Feb 22, 2018
@mhegazy mhegazy added this to the TypeScript 2.8 milestone Feb 22, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

6 participants