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

#31968 - PathValue #31969

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions questions/31968-medium-pathvalue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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:

```typescript
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'
```
7 changes: 7 additions & 0 deletions questions/31968-medium-pathvalue/info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
difficulty: medium
title: PathValue
tags: object, inference, generic, recursive, conditional
author:
github: jw-r
name: 류정우

1 change: 1 addition & 0 deletions questions/31968-medium-pathvalue/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type PathValue<T, P extends string> = any;
19 changes: 19 additions & 0 deletions questions/31968-medium-pathvalue/test-cases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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;