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

Allow proxies to to bypass pedantic index signatures #53892

Closed
5 tasks done
rettgerst opened this issue Apr 18, 2023 · 2 comments
Closed
5 tasks done

Allow proxies to to bypass pedantic index signatures #53892

rettgerst opened this issue Apr 18, 2023 · 2 comments
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

Comments

@rettgerst
Copy link

rettgerst commented Apr 18, 2023

Suggestion

πŸ” Search Terms

typescript proxy pedantic index signatures

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ 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:

const p = new Proxy({}, { get: () => Math.random() }) as Record<string, number>;

const { foo } = p;

foo.toString(); // Object is possibly 'undefined'.

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. with noUncheckedIndexedAccess enabled, all entries are typed as number | 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:

import env from '@rettgerst/env-proxy';

const { DB_PORT } = env.required.int;

env.required.int is a proxy which reads and parses process.env[key] and will either return a number or throw.

under typescript defaults, DB_PORT is correctly typed as number. with noUncheckedIndexedAccess enabled, DB_PORT is incorrectly typed as number | 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.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Apr 18, 2023
@Josh-Cena
Copy link
Contributor

Duplicate of #47594

@rettgerst
Copy link
Author

yep, other issue describes the same request as mine. sorry I didn't see it before, closing this one now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

3 participants