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.

Missing error when mistyped value overwrites property of spreaded generic type parameter

See original GitHub issue

Bug Report

🔎 Search Terms

generic argument, rest spread, false positive

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about object spread and generics

I found a few relevant issues, but none of them fully correspond to the current one:

⏯ Playground Link

Playground link with relevant code

💻 Code

interface InputProps {
  value: string;
}

const getProps = (props: InputProps): InputProps => ({
  ...props,
  value: 123, // when there is no generic type, it shows an error, as expected
});

const getPropsGenericNoSpead = <T extends InputProps>(props: T): T => {
  const newProps = {...props};
  newProps.value = 123; // also error, as expected
  return newProps;
};


const getPropsGeneric = <T extends InputProps>(props: T): T => ({
  ...props,
  value: 123, // no error here
});

🙁 Actual behavior

Typescript allows assigning incompatible types

🙂 Expected behavior

Typescript shows an error

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:5

github_iconTop GitHub Comments

3reactions
teynarcommented, Nov 14, 2022

I am also experiencing this issue and I feel quite surprised since this looks like a fairly common case when using reduce with the spread operator.

function foo<T extends string>(keys: T[]): Record<T, number> {
  return keys.reduce((acc, key) => {
    return {
      ...acc,
      [key]: 'string' // this is not a number, but there is no error
    }
  }, {} as Record<T, number>);
}

TypeScript Playground

0reactions
misuken-nowcommented, Oct 7, 2021

In situations where generics are not used, the use of “Object.assign” may be a solution.

function test(param: { [key: string]: (string | number) }) {
  // Type '{ foo: string; } & { [key: string]: string | number; }' is not assignable to type '{ [key: string]: string; }'.
  // 'string' index signatures are incompatible.
  //   Type 'string | number' is not assignable to type 'string'.
  //     Type 'number' is not assignable to type 'string'.(2322)
  const stringValues: { [key: string]: string } = Object.assign({}, {
    foo: 'bar'
  }, param);
  console.log(stringValues);
}

playground

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does TypeScript complaint about missing property in ...
It complaints that F is not assignable to type Bar , and I don't understand why. ... I'd say you've misplaced the generic...
Read more >
TypeScript errors and how to fix them
error TS1337 : An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type instead....
Read more >
Linter rules - Dart
Avoid types as parameter names. This rule is available as of Dart 2.0.0. Rule sets: core, recommended, flutter. This rule has a quick...
Read more >
C++ Core Guidelines - GitHub Pages
The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++.
Read more >
Control flow and error handling - JavaScript - MDN Web Docs
catch statements. throw statement; try...catch statement. Exception types. Just about any object can ...
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