microsoft / microsoft/TypeScript

Autocompletion fails - unnecessarily narrow for inferred Generic

Open
#52,726 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: LS: Completion Lists Experience Enhancement Help Wanted Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

In the following example typescript IS able to judge the set of valid strings, (as shown by the compiler error), but somehow autocompletion from the language server CAN'T resolve that set. It resolves instead to the set of strings already populated, as shown below in screenshots from the typescript playground

image

image

image

Ideally this approach should have editor autocompletion support constraining strings to type Role, NOT type Evidenced. The type Evidenced is actually inferred from the items being edited right now so the programmer should be able to draw from the broader set Role.

🔎 Search Terms

Autocompletion, inferred, generic, constraint, "too narrow"

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about inference
⏯ Playground Link

Playground link with relevant code

💻 Code
const ROLES = ["artist", "baker", "candlestickmaker"] as const satisfies ReadonlyArray<string>;
type Role = typeof ROLES[number];

function tag<Evidenced extends Role>(
  fn: (evidence: (role: Evidenced) => void) => void,
  ...roles: [Evidenced, ...Evidenced[]]
) {
  // decorate fn
  return () => {;};
}

const decoratedFn = tag(
  () => {;},
  "artist", "baker", "candle"
)
🙁 Actual behavior

The set of strings for autocompletion is initially empty, when it should be populated by Role. After one or more entries have been provided to the list, it then autocompletes only to strings which are already provided.

🙂 Expected behavior

The set of strings for autocompletion of the arguments to the tag function should be populated by Role

Background

The motivating example is within a portfolio of narrative content (interactive fiction-style) using Generator functions. This wrapper function defines a composition function type to evidence roles either by invoking the callback between yields (when a yielded narrative references that role), or if that hasn't happened by calling the callback for those roles when the function eventually returns (allowing very terse definitions for simple narratives without requiring inline callbacks).

This ensures that not referencing at least one role is an error, and offers the delight that narrative elements which touch on some role can lead to an immediate update of the displayed model (showing which roles have been evidenced).

The central purpose of this typing is to assist with editor support, so not being able to have autocompletion is a real dead-end.

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 with the linked TypeScript Playground repro and the generic tag function shown in the issue, then inspect how language-server completion resolves the rest parameter constraint. Done means completion for tag arguments includes the valid Role strings rather than only strings already inferred for Evidenced.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.