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

Record from noUncheckedIndexedAccess: false can't be expressed when noUncheckedIndexedAccess: true #48917

Closed
5 tasks done
safareli opened this issue May 2, 2022 · 4 comments
Closed
5 tasks done
Labels
Duplicate An existing issue was already created

Comments

@safareli
Copy link

safareli commented May 2, 2022

Suggestion

Like with ways for adding and removing optionality form properties ?/- there seams to be a need to remove the special undefinedness of properties introduced in noUncheckedIndexedAccess:true

🔍 Search Terms

noUncheckedIndexedAccess Record Array

✅ 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.

📃 Motivating Example

With noUncheckedIndexedAccess:false we have a notion of Record that defines values, for all the possible properties. In practice there are not many actual values like that. Point ofnoUncheckedIndexedAccess:true was to automatically add | undefined type to result of property access so we are more type-safe automatically.

The main TypeSafe use-case for the records which do have value for any possible key are Proxies. And looks like we have lost a way to express the those properly with noUncheckedIndexedAccess:true

💻 Use Cases

Imagine

const val = new Proxy<{[k in string]: string}>({}, {
  get: (_, p) => p.toString().toUpperCase()
});

const foo = val.foo

with noUncheckedIndexedAccess:false, typer of foo is string tho with noUncheckedIndexedAccess:true, foo has type: string | undefined which is not correct. here we know 100% that any value we read from val via property access, must be string, but it can't be expressed using Typescript when noUncheckedIndexedAccess:true.

We can visualize what we lost using this table:

| noUncheckedIndexedAccess:false | Record<string,string> | Record<string,string | undefiend> | 
| noUncheckedIndexedAccess:true  | _____________________ | Record<string,string>             |
@jcalz
Copy link
Contributor

jcalz commented May 3, 2022

You note that, in practice, such proxy-all-the-things values are rare. That seems to imply that this suggestion would only rarely help people. Am I missing something?

@andrewbranch
Copy link
Member

Duplicate of #47594

Also related: #46969 (comment)

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label May 3, 2022
@safareli
Copy link
Author

safareli commented May 3, 2022

that kind of proxy might not be that common tho opting out from noUncheckedIndexedAccess:true per type level us usefull. i.e. you want noUncheckedIndexedAccess:true for 99% of the types but for some you don't. good use case for this is importing class names from scss/css module file in react code

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants