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

Type Error in Array.includes() method when the array contains only string keys #58671

Closed
LoganTann opened this issue May 27, 2024 · 4 comments
Closed

Comments

@LoganTann
Copy link

πŸ”Ž Search Terms

"typescript for in string" "typescript keyof for in" "array includes keyof"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "for in", "keyof"

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/MYGwhgzhAEAiYBcwGFxWgbwFDV9ADgK4BGIAlsNGAPwBc0ECATmQHYDmA3DnkaRdGJ1orQgFtiAUybcAvlmAB7Vo2hl2rRU0kATANKSAnjHoAKANZHFAMziIUaCAEoA2gF1oAXjwuA5GF83bgVlVR1gAEkVJFZgSS8RSQB3OyRUSAhTJ25wqMYwWMkAOjAEgCJlSTKcyOiCuKLiBIAmbmstaFMlaOhLQzVWaFy6wqdMHlwyW1MAQnVNbX0jCCK2UEIdSUy+pzHsPAPobohFEGKQRXZTMuH8wpcAUggPbyeygBpeo0+AZWY2K63GJxFx9Ny7bgHeTyLBAA

πŸ’» Code

class DataClass {
    public a?: string;
    public b?: number;
}
const ignoredKeys : (keyof DataClass)[] =   ['a'];

const dcInstance = new DataClass();
dcInstance.a = "one";
dcInstance.b = 2;
for (const key in dcInstance) {
    if (!ignoredKeys.includes(key)) {
        console.log("dcInstance[%s] = %s", key, String(dcInstance[key]));
    }
}

πŸ™ Actual behavior

Actual behavior :

  • In statement ignoredKeys.includes(key), error Argument of type 'string' is not assignable to parameter of type 'keyof DataClass'
  • In statement dcInstance[key], error Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'DataClass'. No index signature with a parameter of type 'string' was found on type 'DataClass'.
  • const key is typed as "string"

πŸ™‚ Expected behavior

The statement ignoredKeys.includes(key) should not throw any type errors. The Array.includes() method should accept strings as arguments when all items stored in the ignoredKeys array are string keys.

Other expected behavior, but not important at all :
- Variable key should be of type keyof typeof dcInstance instead of "string"
- No type errors in statement dcInstance[key]

Additional information about the issue

I have an entity and would like to iterate over the keys of that object, excluding some keys.

@MartinJohns
Copy link
Contributor

Duplicate of #14520.

@nmain
Copy link

nmain commented May 27, 2024

ignoredKeys.includes(key) should not throw any type errors

See #14520, and other duplicates linked from it. My search terms were "array includes lower bounds".

Variable key should be of type keyof typeof dcInstance instead of string

Types are not sealed, so this is not true in general. See #12936 for more details.

No type errors in statement dcInstance[key]

This follows from the above; once key is string, the indexing is no longer safe.

@LoganTann
Copy link
Author

Linking another duplicate, thanks for the clarifications.

#55090

Closing this issue.

@LoganTann LoganTann closed this as not planned Won't fix, can't repro, duplicate, stale May 28, 2024
@LoganTann
Copy link
Author

Except casting using unknown or any types, are there any workarounds for the simple example I provided here ?

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

No branches or pull requests

3 participants