-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow for optional index signatures (for the sake of exactOptionalPropertyTypes
)
#46969
Comments
This is effectively a design limitation. We don't support the notion of optional index signatures (i.e. it isn't possible to specify a |
Is there a way to detect that a type is indexed (but not mapped) as a workaround? Update: I ended up with the following. If anyone stumbles upon this and has an idea for improvement please let me know: type IsIndexed<T> = {
[IndexType in string | number | symbol]: IndexType extends keyof T
? true
: false;
}[keyof T];
interface Test {
prop: string;
}
const value: IsIndexed<string> = false;
const object: IsIndexed<Test> = false;
const mapped: IsIndexed<Record<keyof Test, unknown>> = false;
const indexed: IsIndexed<Record<keyof Test | string, unknown>> = true;
const indexedWithoutString: IsIndexed<Record<number | symbol, unknown>> = true; |
If we were able to use // Use Partial since ranges might not be set for all keys
type RangeDictionary = Partial<{ [key: string]: { start: number, end: number }}>;
function getRangeEnds(rangeDictionary: RangeDictionary) {
// TypeScript wants me to consider the possibility that range is undefined here
return Object.values(rangeDictionary).map(range => range.end);
} |
exactOptionalPropertyTypes
: Partial
of index signature adds undefined
exactOptionalPropertyTypes
)
Destructuring + Partial also doesn't work: function foo(v: Partial<{ a: number }>) {}
function boo({ a }: Partial<{ a: number }>) {
// Argument of type '{ a: number | undefined; }' is not assignable
// to parameter of type 'Partial<{ a: number; }>' with 'exactOptionalPropertyTypes: true'
foo({ a })
} Should that be tracked here or should it be a different issue? |
I have encountered the same problem and cannot find a solution anywhere. Has anyone managed to solve it? |
This is NOT just for the sake of This can lead to bugs that I only discover at runtime. Being able to use |
I have a related proposal - is it possible to report with |
Bug Report
🔎 Search Terms
exactOptionalPropertyTypes
strictoptionalProperties
Partial
index signature dictionary record🕗 Version & Regression Information
exactOptionalPropertyTypes
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
See code comments above.
🙂 Expected behavior
See code comments above.
This was briefly discussed in #44524 and #44421 (comment).
The text was updated successfully, but these errors were encountered: