Union in a computed property allows any assignment to property value
See original GitHub issueTypeScript Version: 4.0.0-dev.20200518
Search Terms: computed property, union
Code
type Key = 'a' | 'b';
const index1: Record<Key, string> = { a: '', b: 0 }; // Errors as expected, ok
const index2: Record<Key, string> = { a: '', b: '' };
const b: Key = 'b';
const index3: Record<Key, string> = { ...index2, [b]: 0 }; // Errors as expected, ok
declare var k: Key;
// No errors, allowing anything to be assigned to string
const index4: Record<Key, string> = { ...index2, [k]: [] };
const index5: Record<Key, string> = { a: '', b: '', [k]: 0 };
Expected behavior:
Expected error for invalid assignments in index4
and index5
.
Actual behavior: No error, allowing anything to be assigned to string
Playground Link: here
Related Issues: https://github.com/microsoft/TypeScript/issues/36920: perhaps the computed property is deemed an excess property, though I don’t think it should be
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Is there a way to declare a union type in TypeScript with ...
Is there a way to declare a union type in TypeScript with computed strings based on existing constants? · The Task: · What...
Read more >Computed Properties - Vue.js
Think of a computed property as declaratively describing how to derive a value based on other values - its only responsibility should be...
Read more >NJ Guide to Affordable Housing 2022 UNION COUNTY - NJ.gov
NJ Guide to Affordable Housing 2022. UNION COUNTY o source development / aka street type tenure units area phone agent agent address area_2...
Read more >SECTION 1: EVALUATING BORROWER INCOME CHAPTER 4
eligible for a loan; (2) calculate the applicant's ability to repay a loan; and (3) determine the ... law for property taxes paid...
Read more >Part 31 - Contract Cost Principles and Procedures
Actual cash value means the cost of replacing damaged property with other property of ... and that assigns the cost of such benefits...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here’s my minimal example, similar to OP’s:
So, there’s two things, first:
A computed property name with a non-literal name produces an index signature.
getSpreadType
in the compiler elides the index signature from the output type if either input elides the index signature. So you’ll note in the above, no type has an index signature - the types introduced by the computed names simply evaporate. Now… including it is a little awkward, as it can easily produce a type likewhere the index signature does not actually cover all the members in the type, but that can be fixed.
Second,
Record<Key, string>
is{a: string, b: string}
- when{[x: string]: whatever, a: string, b: string}
is assigned to it, the index signature is not considered to be an excess property. This is done for good reason! If the computed name only actually edits the known names, then there aren’t any excess properties (and index signatures being as wishy-washy as they are, that’s how we prefer it). Since the index signature can’t be excess, the desired error must come from property assignability - which, when you look at it, you realize something’s missing - the issue is that the computed property doesn’t edit the types of the properties it may overwrite!