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

Multiple dynamic property types be ignored #53982

Closed
samchon opened this issue Apr 24, 2023 · 2 comments
Closed

Multiple dynamic property types be ignored #53982

samchon opened this issue Apr 24, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@samchon
Copy link

samchon commented Apr 24, 2023

Bug Report

πŸ”Ž Search Terms

multiple dynamic property types

πŸ•— Version & Regression Information

Tried in those versions:

  • TS 4.9.5
  • TS 5.0.4
  • TS 5.10 dev-20230424

⏯ Playground Link

πŸ’» Code

1st way

type IElement = {
    [P in keyof Record<any, any>]?: P extends "children"
        ? IElement[]
        : IElement;
};

const element: IElement = {
    children: [{}, {}],
    something: {},
};
console.log(element);

2nd way

type IElement = {
    children?: IElement[];
    [key: string]?: IElement;
};

const element: IElement = {
    children: [{}, {}],
    something: {},
};
console.log(element);

πŸ™ Actual behavior

I hope that children: IElement[] type works

πŸ™‚ Expected behavior

TS compiler can't identify children: IElement[] type, and just enforce to use only [key: string]: IElement type.

I can't sure this is a bug that TS compiler failed to detect multiple dynamic property types, or a spec do not allowing it.

@fatcerberus
Copy link

This is not currently possible. You want #17867

@samchon
Copy link
Author

samchon commented Apr 24, 2023

@fatcerberus Ah, this is called hybrid type. Thanks for your link.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 24, 2023
@samchon samchon closed this as completed Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants