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

PathValue #31968

Open
jw-r opened this issue Jan 31, 2024 · 2 comments · May be fixed by #31969
Open

PathValue #31968

jw-r opened this issue Jan 31, 2024 · 2 comments · May be fixed by #31969
Labels
new-challenge Propose a new challenge, a PR will be auto generated

Comments

@jw-r
Copy link

jw-r commented Jan 31, 2024

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

Basic info of your challenge questions,

difficulty: medium
title: PathValue
tags: object, inference, generic, recursive, conditional

Question

In this challenge, you are tasked with creating a type PathValue that will take an object and a string representing the path to a certain value within the object. Your job is to return the type of the value at that path. This will test your skills in generics, conditional types, and type inference.

Here's an example to get you started:

const data = {
  user: {
    name: 'Alice',
    age: 30,
    address: {
      city: 'Wonderland'
    }
  }
} as const;

// The PathValue type should return the type of the value at the specified path.
type UserNameType = PathValue<typeof data, 'user.name'>; // Type: 'Alice'

Template

This is the template for challengers to start the coding. Basically, you just need to change the name of your generic/function and leave to implementation any.

type PathValue<T, P extends string> = any;

Test Cases

Provide some test cases for your challenge, you can use some utils from @type-challenges/utils for asserting.

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

type cases = [
  Expect<Equal<PathValue<typeof data, 'user.name'>, 'Alice'>>,
  Expect<Equal<PathValue<typeof data, 'user.address.city'>, 'Wonderland'>>,
  Expect<Equal<PathValue<typeof data, 'user'>, typeof data.user>>,
  Expect<Equal<PathValue<typeof data, ''>, never>>,
  Expect<Equal<PathValue<typeof data, 'user.number'>, never>>,
]

const data = {
  user: {
    name: 'Alice',
    age: 30,
    address: {
      city: 'Wonderland',
    },
  },
} as const;
@jw-r jw-r added the new-challenge Propose a new challenge, a PR will be auto generated label Jan 31, 2024
github-actions bot pushed a commit that referenced this issue Jan 31, 2024
@github-actions github-actions bot linked a pull request Jan 31, 2024 that will close this issue
Copy link
Contributor

github-actions bot commented Jan 31, 2024

#31969 - Pull Request updated.

2024-02-01T17:26:19.734Z Preview in Playground

github-actions bot pushed a commit that referenced this issue Feb 1, 2024
@antfu
Copy link
Member

antfu commented May 3, 2024

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.

2 participants