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

Inconsistent Behavior with readonly in Template Literal Indexed Access Types #58035

Closed
13OnTheCode opened this issue Apr 2, 2024 · 3 comments
Assignees
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@13OnTheCode
Copy link

๐Ÿ”Ž Search Terms

"readonly" "template literal" "index signatures"

๐Ÿ•— Version & Regression Information

v5.4.3

โฏ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAogHgQwLZgDbQLxQN4CgpQDacAXFAAYAk2AzsAE4CWAdgOYC+AtMAo6uQF0ydJm3xFSFABYQEAE07URLDoOEMV4+rLkB7ZqhASy5GfMW0NbLjz5qoysQW3z9h4xREXHqoQ6usuOy4uHIQAMaoCNpQ4fp0UMAQdGTwyGgQIUl0hADkXlIIhbkCUFi5AIwATADMuVnJwHlmCoWF3LyoJWVQlbX1uKCQUAAqFT0ACozhANYAPGko6AA0UDMQILoAZrCISxAAfLgA9McEUAB6APy4QA

๐Ÿ’ป Code

type Example = {
  [x: `${string}-tail`]: string
  [x: `head-${string}`]: string
  readonly [x: `head-${string}-tail`]: string
  readonly [x: `str-${string}`]: string
}

declare const test: Example

test['str-haha'] = '123' // Expected: error , Actual: error

test['head-haha-tail'] = '123' // Expected: error , Actual: no error

type T1 = Pick<Example, keyof Example>
// {
//    [x: `${string}-tail`]: string;
//    [x: `head-${string}`]: string;
//    [x: `head-${string}-tail`]: string;
//    readonly [x: `str-${string}`]: string;
// }

๐Ÿ™ Actual behavior

I have encountered an inconsistent behavior when using readonly with template literal indexed access types in TypeScript. Specifically, when a string template index signature extends another string template index signature, attempts to mark it as readonly seem to be ignored in the resulting type when using Pick<Type, keyof Type>.

๐Ÿ™‚ Expected behavior

The keyword readonly should be retained, or an error prompt should be given when adding it at the beginning.

Additional information about the issue

No response

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Apr 11, 2024
@ahejlsberg
Copy link
Member

This is working as intended. We consider an element access to be read-only when all applicable index signatures have a readonly modifier. In the example, the string "head-haha-tail" matches three index signatures, two of which are not read-only. Therefore, the assignment is permitted. A similar thing happens when resolving the properties of the Pick<Example, keyof Example> mapped type. For each generated index signature we query the combined read-only-ness of applicable index signatures in the source type, and `head-${string}-tail` matches three, two of which aren't read-only.

@ahejlsberg ahejlsberg added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Needs Investigation This issue needs a team member to investigate its status. labels Apr 12, 2024
@13OnTheCode
Copy link
Author

@ahejlsberg I appreciate your response.

Sorry I didn't make it clear before, I understand that the behavior is working as expected, but my confusion is:

The string index head-${string}-tail matches three index signatures, two of which are not read-only. So, when we try to add readonly to head-${string}-tail only, there should be an error prompt, but the actual situation seems to be correct.

ๅพฎไฟกๆˆชๅ›พ_20240414192355

@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 Apr 17, 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

4 participants