Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In a union, types with index signatures should contribute types to all properties #21141

Closed
sandersn opened this issue Jan 11, 2018 · 1 comment
Assignees
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@sandersn
Copy link
Member

Currently, putting a type with nothing but a string index signature in a union prevents all properties from being accessible. Instead, the type should contribute its index type to all properties:

type U = { foo: { bar: true }, baz: true } | { [key: string]: string };
declare var u: U;
u.foo
u.baz

Currently, foo and baz are not found on u. But they should be present, and have types { bar: true } | string and true | string respectively.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Jan 11, 2018
@RyanCavanaugh RyanCavanaugh added Committed The team has roadmapped this issue and removed In Discussion Not yet reached consensus labels Feb 6, 2018
@RyanCavanaugh
Copy link
Member

@sandersn let's give it a try

@mhegazy mhegazy added this to the TypeScript 2.8 milestone Feb 6, 2018
@mhegazy mhegazy modified the milestones: TypeScript 2.8, TypeScript 2.9 Mar 9, 2018
sandersn added a commit that referenced this issue Jun 28, 2018
This means that in a union like this:

```ts
type T = { foo: number } | { [s: string]: string }
```

`foo` is now a property of `T` with type `number | string`. Previously
it was not.

Two points of interest:

1. A readonly index signature makes the resulting union property readonly.
2. A numeric index signature only contributes number-named properties.

Fixes #21141
@sandersn sandersn added the Fixed A PR has been merged for this issue label Jun 28, 2018
sandersn added a commit that referenced this issue Jul 6, 2018
* Index signatures contribute properties to unions

This means that in a union like this:

```ts
type T = { foo: number } | { [s: string]: string }
```

`foo` is now a property of `T` with type `number | string`. Previously
it was not.

Two points of interest:

1. A readonly index signature makes the resulting union property readonly.
2. A numeric index signature only contributes number-named properties.

Fixes #21141

* Correctly handle numeric and symbol property names

1. Symbol-named properties don't contribute to unions.
2. Number-named properties should use the numeric index signature type,
if present, and fall back to the string index signature type, not the
other way round.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Committed The team has roadmapped this issue Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants