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

Template Literal Types as key types get lost when implementing Interface #49889

Closed
RS-Sautter opened this issue Jul 13, 2022 · 5 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@RS-Sautter
Copy link

Bug Report

πŸ”Ž Search Terms

template literal key remapping interface inheritance

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about template literal types.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type OnString = `on${string}`;

type EventMapping = {
  [key: OnString]: string
}

const map: EventMapping = {
  onClick: 'click'  // ok
}

interface ProvidesEventMapping {
  eventMap: EventMapping
}

class MyComp implements ProvidesEventMapping {
  eventMap = {
    click: 'click'  // this should fail, but doesn't!
  }
}

πŸ™ Actual behavior

no TS error when defining key click.

πŸ™‚ Expected behavior

Error:

Type '{ click: string; }' is not assignable to type 'EventMapping'.
Object literal may only specify known properties, and 'click' does not exist in type 'EventMapping'.
@MartinJohns
Copy link
Contributor

Duplicate of #10570.

The type of your eventMap property is not taken from the implementing interface. It's inferred, and then checked whether it's compatible with the interface, and the type you have is compatible.

When you add an explicit type annotation you will get the excess-properties check: eventMap: EventMapping = {...}

@RS-Sautter
Copy link
Author

@MartinJohns thanks for the explanation. What would be a way in TS to achieve what I would like to do here then? I would not want to explicitly redeclare the type of eventMap but I would want to enforce it to be EventMapping.

@MartinJohns
Copy link
Contributor

TypeScript is structurally typed. Additional properties that are not incompatible with the declared type are always allowed. You would need #12936 to disallow additional properties.

@fatcerberus
Copy link

What would be a way in TS to achieve what I would like to do here then? I would not want to explicitly redeclare the type of eventMap but I would want to enforce it to be EventMapping.

There's no way to do this currently when implementing an interface. The types of class members are inferred the same way regardless of whether the class has an implements clause or not. See #32082.

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

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

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