Typescript Types, unexpected behavior when combining mapped type created from Union, that is used within conditional type

I came across some unexpected behavior when trying to expand mapped type I created from an union of object types, when trying to use this type within conditional type: Playground

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export type CompErrors = {
type: "compilation_errors"
val: number
}
export type TestErrors = {
type: "test_errors"
}
export type TestInfo = {
type: 'test_info'
}
export type Entities = CompErrors | TestErrors | TestInfo
</code>
<code>export type CompErrors = { type: "compilation_errors" val: number } export type TestErrors = { type: "test_errors" } export type TestInfo = { type: 'test_info' } export type Entities = CompErrors | TestErrors | TestInfo </code>
export type CompErrors = {
    type: "compilation_errors"
    val: number
}

export type TestErrors = {
    type: "test_errors"
}

export type TestInfo = {
    type: 'test_info'
}

export type Entities = CompErrors | TestErrors | TestInfo

I create a Type that transform union of object types into and object type where keys are matching type property’s value.

This works as expected, and when used as function parameter I am forced to fill all the keys.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type EntityMap<Type>
= {
[T in Extract<Type, { type: string }> as T["type"]]: ((par: T) => any) | string | boolean | object
}
type Params = EntityMap<Entities>
const entityMapFn = (params: Params) => {}
// missing test_info prop makes error only here
entityMapFn({
compilation_errors: "",
test_errors: "",
})
</code>
<code>type EntityMap<Type> = { [T in Extract<Type, { type: string }> as T["type"]]: ((par: T) => any) | string | boolean | object } type Params = EntityMap<Entities> const entityMapFn = (params: Params) => {} // missing test_info prop makes error only here entityMapFn({ compilation_errors: "", test_errors: "", }) </code>
type EntityMap<Type>
    = {
    [T in Extract<Type, { type: string }> as T["type"]]: ((par: T) => any) | string | boolean | object
}

type Params = EntityMap<Entities>

const entityMapFn = (params: Params) => {}

// missing test_info prop makes error only here
entityMapFn({
    compilation_errors: "",
    test_errors: "",
})

Next I want to expand this type to change the result type based on condition, I add a boolean check and since this branch is not hit in ParamAlternative type I expect that the resulting type will be exactly the same as in case of Params type above.

This is not the case though, and while I still have an auto-completion with correct types for resulting object, now I don’t need to provide all the properties in the object, since the resulting object type is different than in previous case.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type EntityMapAlternative<Type>
= Type extends boolean ? string : {
[T in Extract<Type, { type: string }> as T["type"]]: ((par: T) => any) | string | boolean | object
}
type ParamsAlternative = EntityMapAlternative<Entities>
const entityMapAlternativeFn = (params: ParamsAlternative) => {}
//no error for missing prop
entityMapAlternativeFn({
compilation_errors: "",
test_errors: (te) => {te.type},
})
</code>
<code>type EntityMapAlternative<Type> = Type extends boolean ? string : { [T in Extract<Type, { type: string }> as T["type"]]: ((par: T) => any) | string | boolean | object } type ParamsAlternative = EntityMapAlternative<Entities> const entityMapAlternativeFn = (params: ParamsAlternative) => {} //no error for missing prop entityMapAlternativeFn({ compilation_errors: "", test_errors: (te) => {te.type}, }) </code>
type EntityMapAlternative<Type>
    = Type extends boolean ? string : {
    [T in Extract<Type, { type: string }> as T["type"]]: ((par: T) => any) | string | boolean | object
}

type ParamsAlternative = EntityMapAlternative<Entities>

const entityMapAlternativeFn = (params: ParamsAlternative) => {}

//no error for missing prop
entityMapAlternativeFn({
    compilation_errors: "",
    test_errors: (te) => {te.type},
})

I was wondering whether the condition doesn’t somehow affect the mapped type below it, so I tried to make a type where conditional type is processed outside of next attempt.

This however yields the same result:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type EntityMapExtracted<Type>
= Type extends boolean ? string : EntityMap<Type>
type ParamsExtracted = EntityMapExtracted<Entities>
const entityMapExtractedFn = (params: ParamsExtracted) => {}
//no error for missing prop
entityMapExtractedFn({
compilation_errors: "",
test_errors: (te) => {te.type},
})
type ParamsExtractedWithBool = EntityMapExtracted<boolean>
const withBool = (params: ParamsExtractedWithBool) => {}
// just check that this accepts only string nothing else
withBool("string accepted")
</code>
<code>type EntityMapExtracted<Type> = Type extends boolean ? string : EntityMap<Type> type ParamsExtracted = EntityMapExtracted<Entities> const entityMapExtractedFn = (params: ParamsExtracted) => {} //no error for missing prop entityMapExtractedFn({ compilation_errors: "", test_errors: (te) => {te.type}, }) type ParamsExtractedWithBool = EntityMapExtracted<boolean> const withBool = (params: ParamsExtractedWithBool) => {} // just check that this accepts only string nothing else withBool("string accepted") </code>
type EntityMapExtracted<Type>
    = Type extends boolean ? string : EntityMap<Type>

type ParamsExtracted = EntityMapExtracted<Entities>


const entityMapExtractedFn = (params: ParamsExtracted) => {}

//no error for missing prop
entityMapExtractedFn({
    compilation_errors: "",
    test_errors: (te) => {te.type},
})

type ParamsExtractedWithBool = EntityMapExtracted<boolean>

const withBool = (params: ParamsExtractedWithBool) => {} 

// just check that this accepts only string nothing else
withBool("string accepted")

What exactly does cause this behavior, that changes the result type even though a branch with boolean condition is not hit?

How can I make this work as intended?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật