#How do I accept a key/value pair as an argument to a class method?
38 messages · Page 1 of 1 (latest)
Why's that?
...i don't even know how to explain that. why do you think it's valid
I'll admit I'm basically trying to replicate some code in the form of a class: first answer of this: https://stackoverflow.com/questions/62446219/typescript-javascript-call-function-by-string-name
I'm new to Typescript, in my head I was passing a key that was of type value and a pointer to a function.
separate parameters are separated by ,
[K: string]: Function is wrapped inside an object there, that's what makes that valid, it's an index signature
what kind of input are you expecting with this
So I tried making it an object and it still didn't play nice.
I'm trying to replicate the symbol table from the post above.
The first answer.
you still need an actual parameter
const magicWand: { [K: string]: Function }
// ^ name ^ type
// ^ index signature, part of the type
do you know js?
Yeah.
so a class method?
magicWand(pair) { // body }
pair being an object parameter I guess.
Is it perhaps an interface that I need to define?
right, so now add type annotations to that method
magicWand(pair: object) { // body }
no, that's not correct
you want pair to be a specifc shape here, so you have to specify that
magicWand(pair: {key: string, func: Function}) { // body }
where did pair go :(
I guess I'm unsure how to annotate sub-properties of an argument.
!hb object types