#Why are properties still optional in Required<T>?

4 messages · Page 1 of 1 (latest)

stray anchor
#

Consider this code:

interface HasOptionalFields {
    field: number;
    optional?: string;
}

function fn<T extends HasOptionalFields = HasOptionalFields>(arg: Required<T>) {
    return arg.optional.charAt(0)
}

I expected arg.optional to be type of string, it is string | undefined. Why is that?

rare ermine
winged lintel
#

i don't think that matters here. the issue is how T can be a subtype of HasOptionalFields. for example this is a legal call given your definition:

fn({ field: 1 })
rare ermine
#

oh, yeah