Skip to content

Mapped types shouldn't transform unknown type #29793

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

Open
falsandtru opened this issue Feb 7, 2019 · 4 comments
Open

Mapped types shouldn't transform unknown type #29793

falsandtru opened this issue Feb 7, 2019 · 4 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@falsandtru
Copy link
Contributor

@ahejlsberg Probably #29740 made this regression.

TypeScript Version: 3.4.0-dev.20190207

Search Terms:

Code

type DeepReadonly<T> =
  T extends void ? T :
  { readonly [P in keyof T]: DeepReadonly<T[P]>; };
type m = { a: unknown }; // Also unknown[].
type i = DeepReadonly<m>;

Expected behavior:

i is { readonly a: unknown; }.

Actual behavior:

i is { readonly a: {}; }.

Playground Link:

Related Issues:

@dragomirtitian
Copy link
Contributor

dragomirtitian commented Feb 7, 2019

@falsandtru Doesn't seem to have to do with it being recursive, unknown is mapped to {} in simple mapped types and in 3.3 as well so its probably not related to #29740

type Mapped<T> = { [P in keyof T]: T[P] }

type m_unknown = Mapped<unknown> // {}
type m_any = Mapped<any> // { [x: string]: any; }
type m_never = Mapped<never> // never 

@ahejlsberg
Copy link
Member

This is definitely not a regression, mapped types have behaved this way since the introduction of the unknown type. That said, it might be more consistent to map unknown to unknown as it would better preserve the top-type aspect of unknown.

@ahejlsberg ahejlsberg added the Suggestion An idea for TypeScript label Feb 7, 2019
@falsandtru
Copy link
Contributor Author

@ahejlsberg I judged that this is a regression since the following code make an error since 3.4.0-dev.20190207:

https://github.com/falsandtru/spica/blob/v0.0.222/src/type.test.ts#L368

@falsandtru
Copy link
Contributor Author

falsandtru commented Feb 13, 2019

The essential issue is mapped types replace unknown type with {} type as follows:

type Mapped<T> =
  { [P in keyof T]: T[P]; };
type a = Mapped<unknown>;

Conditional types are not related.

The difference of the behavior since that day is the return type of DeepReadonly<unknown[]> is changed from any[] to {}[]. Conditional types are needed for this behavior but not essential.

@falsandtru falsandtru changed the title Can't infer unknown type via recursive mapped types in conditional types (regression) Mapped types replace unknown type with {} type Feb 13, 2019
@falsandtru falsandtru changed the title Mapped types replace unknown type with {} type Mapped types don't work with any type and unknown type Jan 3, 2020
@falsandtru falsandtru changed the title Mapped types don't work with any type and unknown type Mapped types don't work with unknown type Jan 3, 2020
@falsandtru falsandtru changed the title Mapped types don't work with unknown type Mapped types shouldn't transform unknown type Jan 3, 2020
@RyanCavanaugh RyanCavanaugh added the Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature label Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants