-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Unable to declare an interface that extends Record<string, nonAnyType>
with additional support functions.
#58256
Comments
The type you're trying to create can't be represented in TypeScript. This would require negated types: #4196 |
If |
The closest you can get is using an intersection type: But be aware that this is an unsound type. It's still possible to access the index signature with a key |
This is not a very good solution as the type is not extendable by another interface, and is also not implementable by a class, which is needed in my project. |
Record
type with key type set to string
Record<string, nonAnyType>
with additional support functions.
I'd consider this a duplicate of #17867. There is no "very good solution" here right now, just various workarounds. |
@jcalz It should not be hard to add a fix, since the methods inherited from the
|
Still belongs in #17867 |
🔎 Search Terms
interface method index accessor
method index type 'string'
🕗 Version & Regression Information
The bug occurs at the latest stable version published on npm.
ts-loader
9.5.1
is also used in the project where this problem occurs.The earlier versions have not been tested, but they are very likely to have the same behavior.
⏯ Playground Link
No response
💻 Code
🙁 Actual behavior
The
Record<Exclude<string, "somestring">, string[]>
type is being treated as aRecord<string, string>
type, which causes following error message at the line where I declared theget
method:It also shows the same error message at the same line when using an index accessor without extending
Record
:🙂 Expected behavior
It should not show any errors since the key
get
is excluded when extending theRecord
interface.Additional information about the issue
I have not found any valid solutions that would allow a
Record<string, nonAnyType>
interface to declare additional methods without making the value of the index accessorany
, which I believe most developers would not like since it makes the code unchecked, and so is hard to place documentations. This could also confuse new developers when making a library, since it loses the advantages of using TypeScript over plain JS.Edit:
The solution of using an intersection type works, but I don't think this design to very logical, as in reality not all keys are allocated by a
Record
like this. Furthermore built-inObject
functions accessible, and can coexist with the interface:whereas custom method declarations can only be a
type
, which makes it un-extendable by anotherinterface
, and un-implementable by aclass
.The text was updated successfully, but these errors were encountered: