Title: Suggestion: Infer literal types for getter only properties that return literals · Issue #11467 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: Infer literal types for getter only properties that return literals · Issue #11467 · microsoft/TypeScript
X Title: Suggestion: Infer literal types for getter only properties that return literals · Issue #11467 · microsoft/TypeScript
Description: Suggestion: Infer literal types for getter only properties that return literals. This suggestion arose from reading #11465 the example below comes directly from that issue. TypeScript Version: nightly (2.0.0-dev.20161008) Code // A *self...
Open Graph Description: Suggestion: Infer literal types for getter only properties that return literals. This suggestion arose from reading #11465 the example below comes directly from that issue. TypeScript Version: nigh...
X Description: Suggestion: Infer literal types for getter only properties that return literals. This suggestion arose from reading #11465 the example below comes directly from that issue. TypeScript Version: nigh...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/11467
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: Infer literal types for getter only properties that return literals","articleBody":"Suggestion: Infer literal types for getter only properties that return literals.\n\nThis suggestion arose from reading #11465 the example below comes directly from that issue.\n\n**TypeScript Version:** nightly (2.0.0-dev.20161008)\n\n**Code**\n\n``` ts\n// A *self-contained* demonstration of the problem follows...\nimport React, { Component } from 'react';\nimport { StyleSheet, Text } from 'react-native';\n\nexport class Sample extends Component\u003c{}, {}> {\n render() {\n return (\n \u003cText style={styles.text}>Hello TypeScript\u003c/Text> // error on styles, see below\n );\n }\n}\n\nconst styles = StyleSheet.create({\n text: {\n position: 'absolute', // cause of the error: string instead of 'absolute'\n top: 0,\n left: 0 \n }\n});\n```\n\nI tried a different workaround than the ones presented by @jovdb in #11465\n\n``` ts\nconst styles = StyleSheet.create({\n text: {\n get position() { return 'absolute'; },\n top: 0,\n left: 0\n }\n});\n```\n\n**Expected behavior:**\nThe type of `text.position` is inferred as `'absolute'`\n**Actual behavior:** \nThe type of `text.position` is inferred as `string`\n\nMy reasoning here is that while the following rules, outlined in #10676, make complete sense in general, they are not ideal for getter only properties\n\n> - In a function with no return type annotation, if the inferred return type is a literal type (but not a literal union type) and the function does not have a contextual type with a return type that includes literal types, the return type is widened to its widened literal type.\n> - The type inferred for a property in an object literal is the widened literal type of the expression unless the property has a contextual type that includes literal types.\n\nI am not sure if both of these rules apply here or only the second.\n","author":{"url":"https://github.com/aluanhaddad","@type":"Person","name":"aluanhaddad"},"datePublished":"2016-10-09T06:09:45Z","url":"https://github.com/microsoft/TypeScript/issues/11467"}
| 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:4a303df1-5a2e-d8c1-66af-ef2d67aa2352 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BFD8:83E6C:6DEFB9:95EE0E:6AFBC8EA |
| html-safe-nonce | 86f692568c20e4311b76752aace328428a431457226c20e25dd1b446bb025076 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRkQ4OjgzRTZDOjZERUZCOTo5NUVFMEU6NkFGQkM4RUEiLCJ2aXNpdG9yX2lkIjoiNDAxNDU5NzM1NjAzOTk0ODUyMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7b03080442f72e508f5cd12e7c502432212d237a2e81740f1ce8652231513373 |
| hovercard-subject-tag | issue:181868554 |
| 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/11467/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2fb199f6e31fe3502ba3c2f68587f08e15b55d6fa50d19a07b71104342148f7a/microsoft/TypeScript/issues/11467 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2fb199f6e31fe3502ba3c2f68587f08e15b55d6fa50d19a07b71104342148f7a/microsoft/TypeScript/issues/11467 |
| og:image:alt | Suggestion: Infer literal types for getter only properties that return literals. This suggestion arose from reading #11465 the example below comes directly from that issue. TypeScript Version: nigh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | aluanhaddad |
| hostname | github.com |
| expected-hostname | github.com |
| None | d440bda0d812801778b27bfe9f7c1ce9ff22420c73f6f95150c4fcbca311c50b |
| 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 | 2c43021aed8695990c86c0eaaedc600bfeffec36 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width