Allow proxies to to bypass pedantic index signatures #53892
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
π Search Terms
typescript proxy pedantic index signatures
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
add a utility type that bypasses pedantic index signatures (#39560) by hinting to the compiler that all keys are guaranteed to be accessible.
or, add better support for proxies so the compiler can infer that all values should be the return type of the proxy's get function.
π Motivating Example
trivial example:
in this example
p
is (or is intended to be used as) an object where any and all keys are accessible and are never undefined. withnoUncheckedIndexedAccess
enabled, all entries are typed asnumber | undefined
which is incorrect, and requires the user to perform unnecessary null checks before using the value.π» Use Cases
for a real-world example: my library https://github.com/rettgerst/env-proxy uses proxies to simplify reading environment variables. it can be used like:
env.required.int
is a proxy which reads and parsesprocess.env[key]
and will either return anumber
or throw.under typescript defaults,
DB_PORT
is correctly typed asnumber
. withnoUncheckedIndexedAccess
enabled,DB_PORT
is incorrectly typed asnumber | undefined
, defeating one of the main goals of my library.I also use this proxy approach in several places in private code. a similar approach could be achieved using a function, but I like using proxies because the code is shorter and aesthetically cleaner. there's value in using proxies to abstract away tedious parsing and null checks of objects.
if this feature were to be implemented, then I could update my library and private code with correct typings, so that consumers could use them as intended while also enjoying the enhanced type checking of the
noUncheckedIndexedAccess
feature.The text was updated successfully, but these errors were encountered: