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

[key: string]: string overrides more specific properties #59495

Closed
Arctomachine opened this issue Aug 1, 2024 · 3 comments
Closed

[key: string]: string overrides more specific properties #59495

Arctomachine opened this issue Aug 1, 2024 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@Arctomachine
Copy link

🔎 Search Terms

"[key: string]: string"

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about [key: string] (one mention and seems to be not it)
  • I was unable to test this on prior versions because it starts at 3.3.3 already

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.0-dev.20240731#code/C4TwDgpgBAKghgJwOYWFAvFA3gWAFBRQD0RUwAFgJYDO+hAxlQDYAmCEAdgFzZ2FSUWPasASUOSANx8AvgG0AutLwyoAMl4EocgNYQQw0eKQLDYicpnL8oSFACSHMAFc0mXFpJQ4HFmSrU3uwCLJzAlPRwTHxe1AD2-vpQ1ORxzqxQAEbQ9HEAtmCIEH7ACRBwjFBxFBAIMaTiIuV+cQBm2noGyUYSpt3mSHyMlKzs3Jr8IWbGyoTySvhW+Pi5HCICTq48ji5uE1DDo5w8clhTUABE5BBMTHEXUDIKADSL1nitzhz04XEc3vR6BAwMB7MAIHkABTARAoYA8eDIVAASn27GAzgQ-xhSOAlmWeAqQJBYIhkPEu2RyiAA

💻 Code

type Target = {
  // this
  children: {
    id: string;
  }[];
} & {
  [key: string]: string;
};

type Input = {
  // and this are identical
  // so they should be compared to each other
  // instead of [key: string]: string
  children: {
    id: string;
  }[];
};

const input: Input = {
  children: [{ id: "hello" }],
};

function acceptItem(target: Target) {
  return target;
}

acceptItem(input);

🙁 Actual behavior

If key: string]: string is present in target type, it overrides more specific fields. Even if both target and input have identically defined fields, it will be used instead and might give error (if this field is not string).

🙂 Expected behavior

As less specific, it should be used as fallback. This way it will allow more specific fields to be compared first and avoid potential type error for valid js.

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Aug 1, 2024

Index signatures don't imply "fallback"; they apply to every property. This isn't a bug in TypeScript, it's the intended behavior. You're looking for the feature requested at #17867.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Aug 1, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants