Skip to content

Intellisense String Unions autocomplete inconsistent with ts #49996

Closed as not planned
@dereekb

Description

@dereekb

Bug Report

🔎 Search Terms

Intellisense
String Unions
Incorrect Autocomplete
Types
Typing

🕗 Version & Regression Information

This bug seems like it has been around since the introduction of String Unions.

The following example is of a type that will merge the keys of the input object into an ascending order (which as I write this bug template I notice it is not lexicographical order), since that's how Typescript uses the individual types in the unions of types. This ordering is desirable since it is predictable. Without this order, the following StringConcatenationOrder type would be unusable, as would the other related types.

⏯ Playground Link

export declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
  ? I
  : never;

export type UnionToOvlds<U> = UnionToIntersection<U extends any ? (f: U) => void : never>;
export type PopUnion<U> = UnionToOvlds<U> extends (a: infer A) => void ? A : never;

export type StringConcatenationOrder<S extends string, SEPARATOR extends string> = PopUnion<S> extends infer SELF
  ? //
    SELF extends string
    ? Exclude<S, SELF> extends never
      ? `${SELF}`
      : // This works because the values of S are always interpreted in ascending lexiographical order
        `${StringConcatenationOrder<Exclude<S, SELF>, SEPARATOR>}${SEPARATOR}${SELF}`
    : never
  : never;

export type KeyCanBeString<K> = K extends number | boolean | string | null | undefined ? K : never;

export type OrderedCommaSeparatedKeysOfObject<T extends object> = OrderedCommaSeparatedKeys<`${KeyCanBeString<keyof T>}`>;
export type OrderedCommaSeparatedKeys<T extends string> = StringConcatenationOrder<T, ','>;

// @showEmit
const object = {
  _: 0,
  b: 0,
  a: 0,
  c: 0,
  1: 0
};

const replaced: OrderedCommaSeparatedKeysOfObject<typeof object> = '_,a,b,c,1';

Workbench Repro

🙁 Actual behavior

Screen Shot 2022-07-22 at 1 28 41 AM

In VS Code Intellisense looks at the object's variable declaration order (_, b, a, c, 1), but ts will look at the ascending "lexicographical" order of the object keys (_,a,b,c,1).

In the workspace you can see it give an error, but ts compiles it. This throws me off sometimes since intellisense will present invalid completions that typescript will reject (correctly). Here's the ts error thrown by Jest:

Screen Shot 2022-07-22 at 2 41 52 AM

🙂 Expected behavior

Intellisense should evaluate the object's keys in the ascending order consistent with ts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions