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

Cannot describe recursive type aliases like JSON in terms of generic mapped types #57034

Open
DanielRosenwasser opened this issue Jan 12, 2024 · 1 comment · May be fixed by #57293
Open

Cannot describe recursive type aliases like JSON in terms of generic mapped types #57034

DanielRosenwasser opened this issue Jan 12, 2024 · 1 comment · May be fixed by #57293
Assignees
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@DanielRosenwasser
Copy link
Member

In the following examples, I've defined 4 different versions of a Json type, where they differ on their description of object types.

Playground link here.

In the first two, I've defined them in terms of mapped types with a concrete key of string, and an object type with a string index signature. TypeScript has no issues with either of those.

On the other hand, the last two are described with Record, or with a helper mapped type fed in a string key type.

// No errors ✅
namespace ObjectIsMappedTypeOnString {
    export type Json =
        | string
        | number
        | boolean
        | null
        | Json[]
        | { [K in string]: Json }
        ;
}

// No errors ✅
namespace ObjectIsIndexSignature {
    export type Json =
        | string
        | number
        | boolean
        | null
        | Json[]
        | { [key: string]: Json }
        ;
}

// Errors ❌
namespace ObjectIsRecord {
    export type Json =
        | string
        | number
        | boolean
        | null
        | Json[]
        | Record<string, Json>
        ;
}

// Error ❌
namespace ObjectIsGenericMappedType {
    type JsonProperties<K extends PropertyKey> = {
        [P in K]: Json
    };
    export type Json =
        | string
        | number
        | boolean
        | null
        | Json[]
        | JsonProperties<Json>
        ;
}

I would expect all of these to work.

@jcalz
Copy link
Contributor

jcalz commented Jan 12, 2024

… uh, duplicate #41164?

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jan 12, 2024
@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Needs Investigation This issue needs a team member to investigate its status. labels Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants