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

Remove nesting level #20744

Open
StefanKern opened this issue Dec 15, 2022 · 1 comment · May be fixed by #20745
Open

Remove nesting level #20744

StefanKern opened this issue Dec 15, 2022 · 1 comment · May be fixed by #20745
Labels
new-challenge Propose a new challenge, a PR will be auto generated

Comments

@StefanKern
Copy link

StefanKern commented Dec 15, 2022

Please follow the template and fill the info. A PR will be auto-generated and always reflect on your changes.

Detailed solution/guide is not required, but please be sure the challenge is solvable.

Info

difficulty: easy #  hard 
title: Overwrtie type with type of child
# tags: union, array # separate by comma

Question

Assuming you have and object of type

{
   "id": number,
   "foo": {
        "id": number,
        "data": {
            "id": number,
            "prop1": any,
            "prop2": any
        }
   }
}

and we want to remove the nesting of the data to get a type

{
   "id": number,
   "foo": {
        "id": number,
        "prop1": any,
        "prop2": any
   }
}

Should also work with deeper levels of nesting e.g.

interface DeepModel {
  'id': number,
  'foo': {
    'id': number,
    'data': {
      'id': number,
      'prop1': any,
      'prop2': any,
      'bar': {
        'id': number,
        'data': {
          'id': number,
          'prop1': any,
          'prop2': any,
        }
      }
    }
  }
}

should be reduced to

interface ReducedDeepModel {
  'id': number,
  'foo': {
    'id': number,
    'prop1': any,
    'prop2': any,
    'bar': {
      'id': number,
      'prop1': any,
      'prop2': any,
    }
  }
}

Template

type ReducedNesting<T> = any

Test Cases

import type { Equal, Expect } from '@type-challenges/utils'

interface Model {
   "id": number,
   "extension": {
        "id": number,
        "data": {
            "id": number,
            "prop1": any,
            "prop2": any
        }
   }
}

type ReducedModel = {
   "id": number,
   "extension": {
        "id": number,
        "prop1": any,
        "prop2": any
   }
}

interface DeepModel {
  'id': number,
  'foo': {
    'id': number,
    'data': {
      'id': number,
      'prop1': any,
      'prop2': any,
      'bar': {
        'id': number,
        'data': {
          'id': number,
          'prop1': any,
          'prop2': any,
        }
      }
    }
  }
}

interface ReducedDeepModel {
  'id': number,
  'foo': {
    'id': number,
    'prop1': any,
    'prop2': any,
    'bar': {
      'id': number,
      'prop1': any,
      'prop2': any,
    }
  }
}

type cases = [
  Expect<Equal<ObjectFromEntries<ReducedModel>, Model>>,
  Expect<Equal<ObjectFromEntries<DeepModel>, ReducedDeepModel>>,
]

typescript playground

PS: sorry I did not figure out how I can make the template as beautiful as the others, but I hope this is an interesting challenge

@StefanKern StefanKern added the new-challenge Propose a new challenge, a PR will be auto generated label Dec 15, 2022
@github-actions github-actions bot linked a pull request Dec 15, 2022 that will close this issue
@github-actions
Copy link
Contributor

github-actions bot commented Dec 15, 2022

#20745 - Pull Request updated.

2022-12-15T15:20:47.905Z Preview in Playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-challenge Propose a new challenge, a PR will be auto generated
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant