-
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
noUncheckedIndexedAccess - disable per type or property (use case: dynamic Proxy which returns value for every prop) #47594
Comments
This is the exact same use case I currently have. I really want this feature. Essentially it boils down to being able to declare a type that guarantees that it has ALL properties of type |
Other (now closed) issue mentioned another, far more common use-case (which I'm currently having): CSS module imports. declare module "*.module.scss" {
const css: {readonly [key: string]: string}
export = css
} That allows to import scss files like this: import * as css from "./my_component.module.scss" And refer to classes in that file like this: let myMangledClassName = css.myClassName The thing is, I don't want for So making it |
@nartallax CSS modules was my last blocker for this feature as well. We ended up using css-modules-typescript-loader which is a Webpack loader that emits |
I also use const { ONE, TWO, THREE } = process.env as Required<Record<string, string>>; Unfortunately, in the example above the Currently, without the const record: Partial<Record<string, string>> = {};
const value = record['prop'];
However, if you add the const record: Required<Record<string, string>> = {};
const value = record['prop'];
Maybe the previous example isn't completely correct, since const record = {} as Required<Record<string, string>>; |
I'm wondering if this can be fixed as well. One should be able to explicitly state for a type, that every indexed key is valid and will return a value other than |
This might also make it easier to gradually enable noUncheckedIndexedAccess. I really like the setting, but when I tried turning it on, I got 100+ compilation errors. Not something that can be fixed in a single PR. |
Suggestion
noUncheckedIndexedAccess
- disable per type or property (use case: dynamic Proxy which returns value for every prop)π Search Terms
noUncheckedIndexAccess, proxy, disable
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Now that #13778 is implemented and it works nicely for most of the cases, increasing type safety, I would like to ask if you would consider providing a way to disable it on a type/prop level.
π Motivating Example
We like the new rule and we enabled it in our codebase. But in some specific cases (e.g. a dynamic Proxy which always returns a value for any property or any dynamic getter) we would like to have a possibility to disable/override it.
I provided a simple example below, but we use it to a greater extent incl. nested proxies, dynamic tree-like structures etc. all the dynamic magic stuff javascript is meant for π
In order to improve DX we would also like to avoid sprinkling non-null assertion operators everywhere we use them, like
const a = foo.something()!.nested()!.etc!
I tried overriding index signature types with
NonNullable
,Required
,Exclude
, index signature modifier-?
etc. but no luck.π» Use Cases
In this simple example,
foo
is a proxy which returns name of dynamically accessed property as string. It will always be a string, so undefined should be excluded.[ playground ]
The text was updated successfully, but these errors were encountered: