Title: Suggestion: Type assertion operator (!) with mapped types · Issue #13253 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: Type assertion operator (!) with mapped types · Issue #13253 · microsoft/TypeScript
X Title: Suggestion: Type assertion operator (!) with mapped types · Issue #13253 · microsoft/TypeScript
Description: TypeScript Version: 2.1.1 Also relates to #12424 I recently spent some time looking at the discussion on a safe navigation ?. operator in typescript/javascript (see #16, and https://esdiscuss.org/topic/existential-operator-null-propagati...
Open Graph Description: TypeScript Version: 2.1.1 Also relates to #12424 I recently spent some time looking at the discussion on a safe navigation ?. operator in typescript/javascript (see #16, and https://esdiscuss.org/t...
X Description: TypeScript Version: 2.1.1 Also relates to #12424 I recently spent some time looking at the discussion on a safe navigation ?. operator in typescript/javascript (see #16, and https://esdiscuss.org/t...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/13253
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: Type assertion operator (!) with mapped types","articleBody":"**TypeScript Version:** 2.1.1 \r\n\r\nAlso relates to https://github.com/Microsoft/TypeScript/issues/12424\r\n\r\nI recently spent some time looking at the discussion on a safe navigation `?.` operator in typescript/javascript (see https://github.com/Microsoft/TypeScript/issues/16, and https://esdiscuss.org/topic/existential-operator-null-propagation-operator). Then it struck me that ES6 proxies could be a solution that didn't require new language operators. Wrote something working together, and also found this existing npm package: https://www.npmjs.com/package/safe-proxy. In summary, you'd be able to write\r\n\r\n```typescript\r\n// Safe (only does actual property access if underlying object is valid)\r\nsafe(a).b.c().d[0]\r\n\r\n// ...instead of the functionally equivalent but unsafe\r\na.b.c().d[0]\r\n```\r\n\r\nNow, how do I type annotate this? At first, I thought maybe the new mapped types in Typescript 2.1 could be used, but then I've come to believe that it isn't possible currently (please let me know how to do this otherwise). What I miss is a generalized way of narrowing optional types under `---strictNullChecks`. \r\n\r\nFor example, let's consider using the `mapObject` from the the [Typescript 2.1 release wiki writeup](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html), with optional properties:\r\n\r\n```typescript\r\nconst names:{ foo: string, bar?: string} = { foo: \"\" };\r\nmapObject(names, s => s.length)\r\n```\r\n\r\nThis results in a TS2345 error (basically `bar` can't be optional)\r\n\r\nNow, back to the `SafeProxy` type. The best I can come up with today is\r\n\r\n```typescript\r\ntype SafeProxy\u003cT> = {\r\n [P in keyof T]: SafeProxy\u003cT[P]&any>;\r\n};\r\n```\r\n\r\n...which is marginally better than just `any`.\r\n\r\nI would want (and need for my example above) to define something like below. NOTE: the assertion operator here is made-up-syntax. I suggested an exclamation mark after the actual type, but this could obviously be debated.\r\n\r\n```typescript\r\ntype SafeProxy\u003cT> = {\r\n [P in keyof T]: SafeProxy\u003cT[P]!>;\r\n};\r\n```\r\n\r\nInterestingly it would kind of enable the reverse of `Partial\u003cT>`:\r\n\r\n```typescript\r\n// existing in lib.d.ts\r\ntype Partial\u003cT> = {\r\n [P in keyof T]?: T[P];\r\n};\r\n\r\n// potential\r\ntype Complete\u003cT> = {\r\n [P in keyof T]: T[P]!;\r\n}\r\n```\r\n\r\nSide note: As I intend the `SafeProxy` class to implement `Symbol.toPrimitive`, no trailing `__value` or similar would be needed. This seems to play well with typescript primitives though\r\n\r\nSome other usages (that I guess all play pretty well with `--strictNullChecks`)\r\n* advanced assertion style functions\r\n* default mechanism functions\r\n","author":{"url":"https://github.com/staeke","@type":"Person","name":"staeke"},"datePublished":"2017-01-02T13:18:29Z","url":"https://github.com/microsoft/TypeScript/issues/13253"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:db3b618c-4e89-8bb4-ce8d-ad1767d1d9f7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CB3A:34EA7D:C2D2DB:FA813F:6B00EB56 |
| html-safe-nonce | a8bbebcc605bf149eb544364ae444ef66509f703874781840e8bc603c879e0b9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjNBOjM0RUE3RDpDMkQyREI6RkE4MTNGOjZCMDBFQjU2IiwidmlzaXRvcl9pZCI6Ijg2MzgxNjIwOTQzNjcyMzg5OTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f563d1c49196e52baf13bda0d8a4d935531c37deef32631ba3f039f887431eb6 |
| hovercard-subject-tag | issue:198334549 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/13253/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d44adc97b71acfaaebf1dd554b6f2f007a84d7b507190b32425be19546b6c5dd/microsoft/TypeScript/issues/13253 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d44adc97b71acfaaebf1dd554b6f2f007a84d7b507190b32425be19546b6c5dd/microsoft/TypeScript/issues/13253 |
| og:image:alt | TypeScript Version: 2.1.1 Also relates to #12424 I recently spent some time looking at the discussion on a safe navigation ?. operator in typescript/javascript (see #16, and https://esdiscuss.org/t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | staeke |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width