Skip to content

Literal Union Types => Typeof Unions in Property Indexes #11020

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
ethanresnick opened this issue Sep 20, 2016 · 2 comments
Closed

Literal Union Types => Typeof Unions in Property Indexes #11020

ethanresnick opened this issue Sep 20, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@ethanresnick
Copy link
Contributor

TypeScript Version: 2.1.0-dev.dev.20160920

Code

import express = require("express");
const app = express();

const method = (someCondition()) ? "get" : "post"; // method is "get" or "post"
app[method](reqHandler);

Expected behavior:
Ideally, app[method] could be inferred as typeof app.get | typeof app.post

Actual behavior:
app[method] gives an implicit any error, since app doesn't have an index signature. Doing (<any>app)[method] removes the error, but leaves app[method] as type any rather than typeof app.post | typeof app.get.

To get proper typing, I can do: (<typeof app.get | typeof app.post>(<any>app)[method]), but that's super ugly.

The below almost works:

const method = (someCondition()) ? app.get.bind(app) : app.post.bind(app);
method(reqHandler);

except that .bind() calls return any.

@RyanCavanaugh
Copy link
Member

Duplicate of #6080 (it even uses get / post 😄 )

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 20, 2016
@ethanresnick
Copy link
Contributor Author

Lol. Closing in favor of that one.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants