Skip to content
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

Allow type casts around shorthand property names #13035

Closed
JoshuaKGoldberg opened this issue Dec 19, 2016 · 2 comments
Closed

Allow type casts around shorthand property names #13035

JoshuaKGoldberg opened this issue Dec 19, 2016 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@JoshuaKGoldberg
Copy link
Contributor

JoshuaKGoldberg commented Dec 19, 2016

Is there any reason shorthand property names ({ a }, etc.) don't allow type casting around the names? It would be a mildly useful feature.

Code

The following object literals are all intended to compile to { foo: foo }:

function take(arg: { foo: string }) { }

let foo: string;

// Allowed
take({ foo });
take({ foo: foo });
take({ foo: foo as string });
take({ foo: <string>foo });

// Syntax errors
take({ <string>foo });
take({ foo as string });

Expected behavior:
These should all compile to the same thing.

Actual behavior:
The lines with type casting in the names are syntax errors.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Dec 19, 2016
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature and removed In Discussion Not yet reached consensus labels Jan 24, 2017
JoshuaKGoldberg pushed a commit to JoshuaKGoldberg/TypeScript that referenced this issue Feb 10, 2018
This PR adds a `type?: TypeNode` property to the `ShorthandPropertyAssignment` interface, and adds it to the allowed tokens for shorthand property assignments in parsing. In checking, if a member type is declared on a shorthand assignment, it is used instead of the name.

Fixes microsoft#13035
@JoshuaKGoldberg
Copy link
Contributor Author

JoshuaKGoldberg commented Feb 10, 2018

I've found variants of catch(error) come up once in a while and get flagged by TSLint for unsafe any usage without type casts:

try {
    return {
        data: riskyOperation(),
        succeeded: true,
    };
} catch (error) {
    return {
        error: error as Error,
        succeeded: false
    };
}

@RyanCavanaugh
Copy link
Member

Somewhat wurprised at the lack of feedback here, but in practice this isn't a huge annoyance for me when I encounter it so maybe that makes sense. I think we want to stay well-clear of this syntactic space to keep room for future TC39.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants