question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot assign property to same type with generic key

See original GitHub issue

TypeScript Version: 3.6.0-dev.20190803

Search Terms: assigning keyof generic key

Code

type A = { a: number }
type B = { b: number }

interface Foo {
    a: A
    b: B
}

declare let target: Foo
declare let source: Foo
declare let key: keyof Foo

target[key] = source[key]

Expected behavior:

I expect it to allow the assignment. Since typeof target and typeof source are the same, I expect typeof target[key] to always be the same as typeof source[key].

Actual behavior:

error TS2322: Type ‘A | B’ is not assignable to type ‘A & B’.

Playground Link: https://www.typescriptlang.org/play/#code/C4TwDgpgBAglC8UDeUCGAuKA7ArgWwCMIAnKAXwChRIoAhBZKAzXQk8iigSy2BIDNUAY2gAxAPbjkFKLLSYYMuczoVKFACYQhAG1TFoOiMCjB9Ac2OYJ4zdr0GoRkwGdxOYiOuS7u-YeMoAGsIEEwQkHF+KBtOM2JLYABtCIBdBjcPERTQ1IogA

Related Issues: #31665 (but it was closed and the comments seem to indicate that this should work 🤔) ping @RyanCavanaugh

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:11
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
RyanCavanaughcommented, Aug 5, 2019

#31445 is the canonical one for this problem.

That said, I think we should revisit since it seems to be coming up reasonably often. Special-casing a[k] = b[k] for identical identifiers k would fix 95% of these and in fact it’s hard to imagine a sound assignment where the ks differed

8reactions
RyanCavanaughcommented, Mar 13, 2020

Approved for the case where

t[x] = s[x]

where x is an “identical reference” (the same function used for CFA). When this happens, relate the assignment using the old “union target” rules about the left-hand side instead of the newer stricter “intersection target” put in place. This would be a special case in checkBinaryExpression. Ping me for clarification if needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript: Cannot assign object to a new property of an ...
I'm trying to figure out Typescript's generics, and I'm missing something very basic about them. const testFunction = <T extends {[key: ...
Read more >
How to fix “Cannot assign to property: 'self' is immutable”
How to fix “Cannot assign to property: 'self' is immutable” ... SwiftUI's views should be structs, which means they are immutable by default....
Read more >
Documentation - Utility Types - TypeScript
Constructs an object type whose property keys are Keys and whose property values are Type . This utility can be used to map...
Read more >
Records - C# reference - Microsoft Learn
You can create record types with immutable properties by using positional ... you can't change the value of value-type properties or the ...
Read more >
Restrictions on Generics (The Java™ Tutorials > Learning the ...
Cannot Instantiate Generic Types with Primitive Types. Consider the following parameterized type: class Pair<K, V> { private K key; private V value; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found