microsoft / microsoft/TypeScript

CLI-only error calling generic function

Open
#27,507 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Mapped Types Needs Human Review
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided TypeScript sample through the command-line compiler and compare its result with editor behavior. Done means both paths handle the generic call consistently without the reported TS2345 diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.