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

5 - Get Readonly Keys #3673

Open
akcyp opened this issue Oct 1, 2021 · 0 comments
Open

5 - Get Readonly Keys #3673

akcyp opened this issue Oct 1, 2021 · 0 comments
Labels
5 answer Share answers/solutions to a question en in English

Comments

@akcyp
Copy link

akcyp commented Oct 1, 2021

5 - Get Readonly Keys

Solution

Code

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

type GetReadonlyKeys<T, R extends T = Readonly<T>> = {
  [K in keyof T]-?: Equal<Pick<R, K>, Pick<T, K>> extends true ? K : never;
}[keyof T];

Test cases

/* _____________ Test Cases _____________ */
import { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<'title', GetReadonlyKeys<Todo1>>>,
  Expect<Equal<'title' | 'description', GetReadonlyKeys<Todo2>>>,
]

interface Todo1 {
  readonly title: string
  description: string
  completed: boolean
}

interface Todo2 {
  readonly title: string
  readonly description: string
  completed?: boolean
}
@akcyp akcyp added answer Share answers/solutions to a question en in English labels Oct 1, 2021
@github-actions github-actions bot added the 5 label Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant