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

#20901 - key_type from props #20902

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
1 change: 1 addition & 0 deletions questions/20901-hard-key_type-from-props/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement type that extracts key and type both from object props. More details please refer to test cases.
7 changes: 7 additions & 0 deletions questions/20901-hard-key_type-from-props/info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
difficulty: hard
title: key_type from props
tags: object-keys
author:
github: lvjiaxuan
name: lvjiaxuan

1 change: 1 addition & 0 deletions questions/20901-hard-key_type-from-props/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type KeyTypeFromProps<T> = any
37 changes: 37 additions & 0 deletions questions/20901-hard-key_type-from-props/test-cases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Equal, Expect } from '@type-challenges/utils'

type obj = {
[x: string]: (
| {
component: 'AInput'
value: string
}
| {
component: 'ASelect'
value: string
}
| {
component: 'ARangePicker'
value: [number, number]
}
) & {
label?: string
}
}

type res = {
AInput: string;
ASelect: string;
ARangePicker: [number, number];
}

type cases = [
Expect<Equal<KeyTypeFromProps<obj, 'component', 'value'>, res>>,

// @ts-expect-error
KeyTypeFromProps<{ x: 1 }, '', ''>,
// @ts-expect-error
KeyTypeFromProps<obj, 'fake', 'fake'>,
// @ts-expect-error
KeyTypeFromProps<[], '', ''>
]