Skip to content

in keyword completionsΒ #52736

@zardoy

Description

@zardoy

Suggestion

πŸ” Search Terms

in keyword completions
in keyword suggestions
in keyword intellisense

βœ… Viability Checklist

My suggestion meets these guidelines:

  • [z] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [z] This wouldn't change the runtime behavior of existing JavaScript code
  • [z] This could be implemented without emitting different JS based on the types of the expressions
  • [z] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • [z] This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Typescript docs says that in operator is usually used for object type narrowing, but sometimes you can forget property name that you want to get after narrowing. Having additional intellisense in these locations can be helpful.

TLDR Problem: having no clue what to complete in the left side of in keyword.

πŸ“ƒ Motivating Example

declare const foo: { a: boolean, b: string } | { a: number, c: number }

if ('' in foo) {}

This is super easy implement and I implemented it in my plugin, but also decided to make as much informative as possible:

image

Numbers on right side of the completions means type index, starting from actual position in the union (starting from 1).
When specific key is selected, completion widget displays the type of key in each type of the union. Star means that this key can narrow type (not present in all types of union).

I understand that sometimes these numbers can be confusing e.g.:

type Fish = /*1*/{ swim: () => void };
type Bird = /*2*/{ fly: () => void } | /*3*/{ swim: () => void };
type Human = /*4*/{ swim?: () => void; fly?: () => void };
 
function move(animal: Fish | Bird | Human) {
  if ("" in animal) {
    animal;
  } else {
    animal;
  }
}

I wonder is this even good idea to display such information, while it is super useful for me, probably it can confuse other users. However I think its worth to display documentation for each completion at least, but probably in other format. eg for swim completion from example above:

From Fish: () => void

From Bird[2]: () => void

From Human: (() => void) | undefined

Anyway its just a proposal, so let me know what you think.

And finally I think new kind of completions should be added like ts.ScriptElementKind.stringNotStrict to show client that it actually accepts arbitrary string.

πŸ’» Use Cases

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureDomain: LS: Completion ListsThe issue relates to showing completion lists in an editorSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions