Skip to content

Inference inconsistency with string keys #26257

Closed
@yortus

Description

@yortus

TypeScript Version: 3.1.0-dev.20180807

Search Terms:
string index inference, string key inference

Code

const SYM = Symbol();
type StringProps1<T> = T extends {[x: string]: infer U} ? U : never;
type StringProps2<T> = T extends {[K in Extract<keyof T, string>]: infer U} ? U : never;

type T1 = StringProps1<{a: 'aaa', [SYM]: 'sym'}>;   // T1 = 'aaa' | 'sym'
type T2 = StringProps2<{a: 'aaa', [SYM]: 'sym'}>;   // T2 = 'aaa'

Expected behavior:
I expected StringProps1 and StringProps2 to mean the same thing (infer from just the string keys).

Accordingly, T1 and T2 would both inferred as 'aaa', both excluding the symbol key.

Actual behavior:

T2 excludes the symbol key as expected, but T1 includes it, as shown in the code comments.

Even if StringProps1 and StringProps2 did have subtly different meanings, it doesn't seem logical to include the [SYM]: 'sym' as part of the string indexer, since it doesn't belong there.

Related Issues:

This is a simpler repro of #26183, although that issue shows its nothing specific to conditional types. It seems to be just how string indexers work with type inference.

The search terms above brought up #25065 and #24560, but they don't look like the same issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions