typescript - Generic type works with function argument for map but not single value -


interface datageneric {     value: number; }  function transform<d extends datageneric>(data: datageneric[], get_value: (record: d) => number) {      // works without error     let values = data.map(get_value);     // following line errors with:     //    argument of type 'datageneric' not assignable parameter of type 'd'.     //     values = data.map(d => get_value(d));     // works without error, why type assertion needed?     values = data.map(d => get_value(d d)); } 

i'm wondering why type assertion needed when passing single value get_value?

typescript 2.3.4

the reason need cast d or error:

argument of type 'datageneric' not assignable parameter of type 'd'

is this:

interface datageneric2 extends datageneric {     value2: string; }  transform([{ value: 3 }], (record: datageneric2) => {     return record.value2.length; }); 

in example, function being passed get_value expects values of type datageneric2 , not datageneric.
error compiler complaining that:
argument of type 'datageneric' not assignable parameter of type 'datageneric2'.

when cast d (or change signature of function) tell compiler know you're doing , ok.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -