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.

CLI-only error calling generic function

See original GitHub issue

TypeScript Version: 3.2.0-dev.20181002

Code

function getFirst<K extends string, T>(obj: Record<K, ReadonlyArray<T>>, key: K): T {
	return obj[key][0];
}

interface I {
	readonly n: ReadonlyArray<number>;
	readonly s: ReadonlyArray<string>;
}

function f(i: I): number {
   return getFirst(i, "n");
}

Expected behavior:

No error.

Actual behavior:

No error in the editor. Error on the command line:

src/a.ts:11:20 - error TS2345: Argument of type 'I' is not assignable to parameter of type 'Record<"n", ReadonlyArray<string>>'.
  Types of property 'n' are incompatible.
    Type 'ReadonlyArray<number>' is not assignable to type 'ReadonlyArray<string>'.
      Type 'number' is not assignable to type 'string'.

11    return getFirst(i, "n");

Related Issues: #19686

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sledorzecommented, Oct 19, 2018

Whats the ‘take the first type’ motivation? (Looks like a can of worms to me)

0reactions
RyanCavanaughcommented, Oct 19, 2018

Rewritten version that is stable between CLI/server

type FetchArr<T, K extends keyof T> = T extends { [x in K]: ReadonlyArray<infer E> } ? E : void;  

function getFirst<T, K extends keyof T>(obj: T, key: K): FetchArr<T, K> {
	return obj[key][0];
}

interface I {
	readonly n: ReadonlyArray<number>;
	readonly s: ReadonlyArray<string>;
}

function f(i: I): number {
  return getFirst(i, "n");
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

c++-cli: Why does calling a generic function fail, when it calls ...
After some testing, I just found a solution to this specific problem: I need to define func1g as follows: generic <class T> void...
Read more >
"All call to function are unreachable" for templated functions
It happens frequently that on templated functions I get this error, but it's silly because, ... OS Linux(amd64) v5.11.0-38-generic, screens 1920.0x1200.0.
Read more >
Confused about calls to generic function : r/fsharp - Reddit
So I'm trying to call this generic function 2 times with different types but the second time, it expects the first type. Can...
Read more >
CLion error (HardwareSerial0) - PlatformIO Community
I made an integration with CLion, I did a brand new project (because ... HardwareSerial0.cpp.o (symbol from plugin): In function `Serial': ...
Read more >
Practical heuristics to improve precision for erroneous function ...
This helps guard against a bad call to the function if the user specified the arguments out of order, resulting in compile-time errors....
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