You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms:
conditional types never keyof required properties
Expected behavior:
In the code below, I would expect the type requiredKeysType1 to be "name" | "email".
Actual behavior:
The type of requiredKeysType1 is actually "name" | "email" | undefined. I had to define the Definable to filter out undefined and I don't understand why. The type requiredKeysType2 shows usage of that type.
My goal here is to define a const array of the required properties on a given interface for use in a type guard (isPerson). I would like the compiler to complain if I remove a key from the Person interface and forget to update the array.
Not implemented here but wished for - I would like the compiler to complain if I add a required property to Person and forget to update the array, but I can't figure out how to make that happen.
Related Issues:
Didn't find any related issues.
Code
interfacePerson{name: stringage?: numberemail: string}typerequiredKeysType1=keyofRequiredProperties<Person>// requiredKeys1 = "name" | "email" | undefinedconstrequiredKeys1: Array<requiredKeysType1>=["name"]typerequiredKeysType2=Definable<keyofRequiredProperties<Person>>// requiredKeys2 = "name" | "email"constrequiredKeys2: ReadonlyArray<requiredKeysType2>=["name"]asconstfunctionisPerson2(s?: object): s is Person{return!!(s&&requiredKeys2.every((k)=>kins))}/** * Remove undefined from the type given (most useful for unions) */typeDefinable<T>=Textendsundefined ? never : T// Due to https://dev.to/busypeoples/notes-on-typescript-conditional-types-4bhtypeRemoveUndefinable<Type>={[KeyinkeyofType]: undefinedextendsType[Key] ? never : Key}[keyofType];/** * Retrieves all the keys for the given object that are required (can't be * undefined.) */typeRequiredProperties<Type>={// for some reason, keyof appends an `undefined` value. `Definable` filters that out.[KeyinRemoveUndefinable<Type>]: Type[Key]};
I'm confused. In the title you say it produces never, but then you don't mention it anywhere, neither in the actual behavior nor the expected behavior. The code doesn't produce a never either. Did you mean to write undefined in your title?
On Sat, Oct 17, 2020 at 12:45 AM Martin Johns ***@***.***> wrote:
I'm confused. In the title you say it produces never, but then you don't
mention it anywhere, neither in the actual behavior nor the expected
behavior. The code doesn't produce a never either. Did you mean to write
undefined in your title?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#41145 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAESAOPB2BLC4Y7B3QTSHTSLFDQLANCNFSM4ST6ZFSA>
.
m4dc4p
changed the title
Conditional Type Produces never Unexpectedly
Conditional Type Produces undefined Unexpectedly
Oct 17, 2020
TypeScript Version: 4.0.2
Search Terms:
conditional types never keyof required properties
Expected behavior:
In the code below, I would expect the type
requiredKeysType1
to be"name" | "email"
.Actual behavior:
The type of
requiredKeysType1
is actually"name" | "email" | undefined
. I had to define theDefinable
to filter outundefined
and I don't understand why. The typerequiredKeysType2
shows usage of that type.My goal here is to define a const array of the required properties on a given interface for use in a type guard (
isPerson
). I would like the compiler to complain if I remove a key from thePerson
interface and forget to update the array.Not implemented here but wished for - I would like the compiler to complain if I add a required property to
Person
and forget to update the array, but I can't figure out how to make that happen.Related Issues:
Didn't find any related issues.
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: