Skip to content

Problems with computed property must be of type 'string', 'number', 'symbol', or 'any'. #17587

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
remojansen opened this issue Aug 3, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@remojansen
Copy link
Contributor

I'm having problems with the restriction:

A computed property name must be of type 'string', 'number', 'symbol', or 'any'.

And the following function:

const selectWhere = <T, W extends keyof T, S extends keyof T>(where: W, select: S) =>
    (entities: T[], value: T[W]) =>
        entities.filter(e => e[where] === value)
                .map(e => ({
                    [select]: e[select] // Error
                }));

The functioin shoud allow me to filter an awway by a property and select some attributes:

interface User {
    surname: string;
    age: number;
}

const selectSurnameWhereAge = selectWhere<User, "age", "surname">("age", "surname");

const users = [
    { surname: "Smith", age: 28 },
    { surname: "Johnson", age: 28 },
    { surname: "Williams", age: 14 }
];

let result = selectSurnameWhereAge(users, 28);

result[0].surname; // "Smith"
result[0].age; // Error
result[1].surname; // "Johnson"
result[2].age; // Error

I have a work around but it is very verbose:

const selectWhere = <T, W extends keyof T, S extends keyof T>(where: W, select: S) =>
    (entities: T[], value: T[W]) =>
        entities.filter(e => e[where] === value)
                .map(e => {
                    let obj = {};
                    Object.keys(e).forEach((k) => {
                        if (k === select) {
                            obj[k] = e[k];
                        }
                    })
                    return obj as Pick<T, S>;
                });

Reproduce it at the Playground.

@RyanCavanaugh
Copy link
Member

Duplicate of #14473 ?

@RyanCavanaugh RyanCavanaugh marked this as a duplicate of #14473 Aug 3, 2017
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 3, 2017
@ahejlsberg
Copy link
Member

How about just:

const selectWhere = <T, W extends keyof T, S extends keyof T>(where: W, select: S) =>
    (entities: T[], value: T[W]) => entities
        .filter(e => e[where] === value)
        .map(e => (<Pick<T, S>>{ [<string>select]: e[select]}));

@remojansen
Copy link
Contributor Author

Thanks for helping out, I just tried with:

const selectWhere = <T, W extends keyof T, S extends keyof T>(where: W, select: S) =>
    (entities: T[], value: T[W]) => entities
        .filter(e => e[where] === value)
        .map(e => (<Pick<T, S>>{ [<string>select]: e[select]})); // Error

But I got the following error in the playground:

Type '{ [x: string]: T[S]; }' cannot be converted to type 'Pick<T, S>'.

I reported it because I really think that it would be awesome if in the future my sample works without the need for casting.

const selectWhere = <T, W extends keyof T, S extends keyof T>(where: W, select: S) =>
    (entities: T[], value: T[W]) =>
        entities.filter(e => e[where] === value)
                .map(e => ({
                    [select]: e[select] // Error
                }));

Feel free to close the issue if you believe that it is a duplicate of #14473 unfortunately I don't know enough about the internals of the type system to be sure about it being a duplicate...

@mhegazy
Copy link
Contributor

mhegazy commented Aug 18, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Aug 18, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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

4 participants