Description
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';
🙁 Actual behavior
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:
🙂 Expected behavior
Intellisense should evaluate the object's keys in the ascending order consistent with ts
.