TypeScript Version: 3.6.3
Search Terms:
array.map, expression is not callable, union type signatures,
Code
The following code is the exact same code as in this codesandbox
https://codesandbox.io/s/lingering-sun-7itgj
interfaceProduct_result{__typename: 'Product'id: numberdescription: string|null}interfaceCampaign_result{__typename: 'Campaign'id: numberproducts: (Product_result|null)[]|null}interfaceProduct{id: numberdescription: stringspecific: boolean}interfaceCampaign{products: Product[]}typeCompoundType=Campaign_result|Campaign/* --- */constprops: {campaign?: Campaign_result}={}constemptyCampaign: Campaign={products: []}constinitialData: CompoundType=props.campaign||emptyCampaign/*Cannot invoke an expression whose type lacks a call signature. Type ' (<U>(callbackfn: (value: Product_result, index: number, array: Product_result[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: Product, index: number, array: Product[]) => U, thisArg?: any) => U[])' has no compatible call signatures.product inside map is any, but I know `id` is there*/initialData.products.map(product=>product.id)// product is { description: string, id: number } - as expectedconstproduct=initialData.products[0]// product inside map is { description: string, id: number } - as expected;(initialData.productsasArray<CompoundType['products'][0]>).map(product=>product.id)/* interestingly */typeT01=Array<CompoundType['products'][0]>// T01: (Product_result | Product)[] - I can actually have mix of both. Products that were fetched and Products added via a FormtypeT02=CompoundType['products']// T02: Product_result[] | Product[]declareconstv1: T01v1.map(product=>product)// OKdeclareconstv2: T02v2.map(product=>product)// NOKExpected behavior:
I know id is there, so this should work
initialData.products.map(product => product.id)
This workaround works
(initialData.products as Array<CompoundType['products'][0]>).map(product => product.id)
Actual behavior:
The map gives a non compatible call signature error (as in the snippet above), and the product inside is any.
Related
TypeScript 3.3 - Improved behavior for calling union types
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-3.html#caveats
TypeScript Version: 3.6.3
Search Terms:
array.map, expression is not callable, union type signatures,
Code
The following code is the exact same code as in this codesandbox
https://codesandbox.io/s/lingering-sun-7itgj
Expected behavior:
I know
idis there, so this should workinitialData.products.map(product => product.id)This workaround works
(initialData.products as Array<CompoundType['products'][0]>).map(product => product.id)Actual behavior:
The map gives a non compatible call signature error (as in the snippet above), and the product inside is any.
Related
TypeScript 3.3 - Improved behavior for calling union types
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-3.html#caveats