Skip to content

Support a fieldof type. #4822

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
Fruneau opened this issue Sep 16, 2015 · 2 comments
Closed

Support a fieldof type. #4822

Fruneau opened this issue Sep 16, 2015 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Fruneau
Copy link

Fruneau commented Sep 16, 2015

In some cases, we may want to have a function that read one field of an object identified by a parameter. Here is a small synthetic use case, for the purpose of the discussion:

interface Help {
    brief: string;
    detailed: string;
    contextual: string;
}

function getHelpString(help: Help, format: string): string
{
    return help[format];
}

In that particular case, the type checker cannot ensure that we are doing things right. In order to make the type checker happy, we need a way to tell it that format is actually part of an implicit enumeration that only contains the names of the fields of the interface Help. This could be done by introducing a new type inference operator, for example fieldof.

function getHelpString(help: Help, format: fieldof Help): string
{
    return help[format];
}

This may not look like a very common use case, but I hoped to have such an operator in order to port incrementally a large amount of javascript code to typescript.

@jbrantly
Copy link

Duplicate of #394

@DanielRosenwasser
Copy link
Member

Thanks for the suggestrion @Fruneau, and thanks for catching that @jbrantly. Closing in favor of #394.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Sep 16, 2015
@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

3 participants