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

When an index signature is not available, encourage using a more specific type to index the type. #36962

Open
5 tasks done
threehams opened this issue Feb 23, 2020 · 3 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

@threehams
Copy link

Search Terms

index signature, keyof, domain: error messages
Vaguely related:
#14951

Suggestion

When there is a type error because of a missing string index signature, add a suggestion of:
"Did you mean to use a more specific type such as keyof Thing instead of string?"

Only show this message:

  • if the type used to index is within the same scope
  • the object being indexed is not empty

Use Cases

Take the following example:

interface Person {
  name: string;
  phone: string;
}
declare const person: Person;

const get = (person: Person, key: string) => {
  return person[key];
};

get(person, "name");

The error message here is:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Person'.
  No index signature with a parameter of type 'string' was found on type 'Person'.

This is technically correct! However, many people read this error message to mean "add an index signature to Person," rather than advising them to use a more specific type than string, like keyof Person. Adding the index signature suppresses the error, making it seem like the correct solution.

Where this doesn't work

There are some cases where this advice would not make sense:

// Did you mean to use a more specific type such as `keyof Number` instead of `string`?
1["a" as string]

Other cases I can think of so far:

  • Empty objects, especially from reduce (suppress for empty objects)
  • Indexing an object by number (only do this for strings?)
  • Object.keys and Object.entries as discussed many, many times (require the indexing type to be in the same scope)

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh RyanCavanaugh added 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 labels Feb 24, 2020
@allandiego
Copy link

allandiego commented May 6, 2020

As a new Typescript user i got confused with this error message, just realized how to solve the error after reading this issue part "keyof Type", then everything made sense, upvote to improve message description

@grant
Copy link

grant commented Apr 21, 2021

In my case, using as keyof MyInterface was the solution:

const unknownKey = myString as keyof MyInterface;
myObject[unknownKey] = ...;

@j0053f
Copy link

j0053f commented Sep 4, 2022

why can't I narrow it with the "in" operator?
e.g.

function get(person: Person, key: string) {
     if (key in person){
        return person[key];
    }
};

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

5 participants