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

Compiler cannot infer the type of the visitor method parameter if you use enum values as the property names. #3

Closed
UselessPickles opened this issue Dec 26, 2017 · 2 comments · Fixed by #8

Comments

@UselessPickles
Copy link
Owner

Cannot infer the type of the visitor method parameter if you use the enum value as the property name.

I reported this issue on the github TypeScript repo and was promptly informed that the issue is already fixed in development for the "next" version of TypeScript (currently named v2.7.0-dev).

There are two work-arounds to this:

  1. Explicity declare the type of the parameter.
  2. Use a string literal for the property name.

Example:

enum RGB {
    R = "r",
    G = "g",
    B = "b"
}

function rgbIdentity(rgb: RGB): RGB {
    return visitString(rgb).with<RGB>({
        // ERROR: type of `value` not inferred properly!
        // Type is implicitly 'any'
        [RGB.R]: (value) => {
            return value;
        },
        // Work-around option #1
        [RGB.G]: (value: RGB.G) => {
            return value;
        },
        // Work-around option #2
        "b": (value) => {
            return value;
        }
    });
}

After the new version fo TypeScript is released, add a unit test for this situation.

@UselessPickles
Copy link
Owner Author

There's a commented-out sample file that needs to be uncommented to enable unit testing of this situation (after upgrading TypeScript): tests/compile_samples/pass/enum.enum-props.inferred-param-type.ts

@UselessPickles
Copy link
Owner Author

This is no longer an issue as of TypeScript 2.7.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant