Skip to content

57 - Get Required #285

Open
Open

Description

@note89

Typescript 4.1

type GetRequired<T> = { [P in keyof T as T[P] extends Required<T>[P] ? P : never]: T[P] };

Activity

  1. added
    answer
    en
    on Sep 29, 2020
  2. Luncher commented on Mar 13, 2021

    @Luncher

    hi @note89 Can you explain the function of as keyword here?

    T[P] is union type,Why don't need to disable distribution ?

    thanks

  3. Dsan10s commented on Apr 13, 2021

    @Dsan10s

    Hi @Luncher , maybe I might be able to help

    The addition of as in mapped index types is a new feature of Typescript 4.1

    It allows you to transform a mapped type like P in keyof T into a new type, for any given P in this case
    One of the features of this is if you transform P into never, it acts as a filter!

    So here, we can actually think of T[P] not as a union type, but rather as an individual instance of what T[P] resolves to for a given P. If T[P] extends Required<T>[P] for a given P, we know for sure the ? operator was not present on this key. Otherwise, the ? was present, so we can filter out the key P by mapping it to never

    Hope this helped!

  4. Luncher commented on Apr 14, 2021

    @Luncher

    @Dsan10s Thank you for your patient reply.. 👍

  5. wine-fall commented on Feb 16, 2022

    @wine-fall

    hi @Dsan10s
    I don't understand why this code get wrong?
    as you explained, as allows me to transform k in keyof T into a new type, but when I add the brackets, It throw lots of error
    image

  6. tjx666 commented on Mar 9, 2022

    @tjx666

    hi @Dsan10s I don't understand why this code get wrong? as you explained, as allows me to transform k in keyof T into a new type, but when I add the brackets, It throw lots of error image

    I think this just a grammar limit. In this situation, keyword as and Key type can't be separated by bracket.

  7. dimitropoulos commented on Jan 16, 2023

    @dimitropoulos
    Contributor

    Thanks for the discussion here, everyone! I'll be featuring this solution and some of the things you said in the video solutions!

  8. tomina1 commented on Feb 20, 2024

    @tomina1
    type GetRequired<O> = { [K in keyof O as undefined extends O[K] ? never : K]: O[K] };

    The non-required attribute will have undefined, and you can filter by using as to determine undefined extends O[K]

  9. skeleton1231 commented on Aug 22, 2024

    @skeleton1231

    type GetRequired = {
    [K in keyof T as T extends Record<K, T[K]> ? K : never]: T[K]
    };

  10. sciencefunq commented on Aug 12, 2025

    @sciencefunq

    type GetRequired = { [K in keyof O as undefined extends O[K] ? never : K]: O[K] };
    The non-required attribute will have undefined, and you can filter by using as to determine undefined extends O[K]

    It is a wrong solution,since required properties may be union type including undefined. for example:
    Expect<Equal<GetRequired<{ foo: undefined, bar?: undefined }>, { foo: undefined }>>,

  11. ingadi commented on Oct 30, 2025

    @ingadi
    type GetRequired<T> = {
      [K in keyof T as {} extends Pick<T, K> ? never : K]: T[K];
    };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    57answeren

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions