Skip to content

Definite assignment assertion doesn't work on object-short hand syntax #23118

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
tinganho opened this issue Apr 3, 2018 · 3 comments
Closed
Labels
Bug A bug in TypeScript Help Wanted You can do this

Comments

@tinganho
Copy link
Contributor

tinganho commented Apr 3, 2018

TypeScript Version: 2.7.0-dev.201xxxxx

Search Terms:
definite assignment assertion object shorthand

Code

interface A {
    a: string;
}

let As: A[] = [];
let a: string | undefined = 'ff';

function f() {
    As.push({ a! }); // Error
}

Expected behavior:
No error

Actual behavior:

Argument of type '{ a: boolean; }' is not assignable to parameter of type 'A'.
  Types of property 'a' are incompatible.
    Type 'boolean' is not assignable to type 'string'.

Playground Link:
http://www.typescriptlang.org/play/#src=interface%20A%20%7B%0A%20%20%20%20a%3A%20string%3B%0A%7D%0A%0Alet%20As%3A%20A%5B%5D%20%3D%20%5B%5D%3B%0Alet%20a%3A%20string%20%7C%20undefined%20%3D%20'ff'%3B%0A%0Afunction%20f()%20%7B%0A%20%20%20%20As.push(%7B%20a!%20%7D)%3B%20%2F%2F%20Error%0A%7D

Related Issues:

@tinganho tinganho changed the title Definite assignments assertion doesn't work on object-short hand syntax Definite assignment assertion doesn't work on object-short hand syntax Apr 3, 2018
@mhegazy mhegazy added Bug A bug in TypeScript Help Wanted You can do this labels Apr 3, 2018
@mhegazy mhegazy added this to the Community milestone Apr 3, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Apr 3, 2018

PRs welcomed.

@ajafff
Copy link
Contributor

ajafff commented Apr 3, 2018

@mhegazy are you sure this is a bug?

This is not a definite assignment assertion. That's a misplaced non-null assertion. There's a parse error at the exclamation mark. Everything is working as expected, only the assignability error is a bit misleading.

You just need to write it as {a: a!}.

#13035 already tracks type assertions on shorthand properties.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 3, 2018

last time we talked about assertions, it seemed like a good idea to allow them. i do not think this is specifically different.
I think the bug here is a parsing bug. we parsed it as a uniary prefix expression instead of a unary postfix expression. and hence the boolean type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants