Hi, I am trying to get proper type for 2nd param in a function whose type should depend upon the first one, here is an example
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
53 messages · Page 1 of 1 (latest)
Hi, I am trying to get proper type for 2nd param in a function whose type should depend upon the first one, here is an example
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
@fringe wolf Here's a shortened URL of your playground link! You can remove the full link from your message.
Preview:```ts
type AppleOptions = {
iron: number
}
type OrangeOptions = {
vitaminC: number
}
function sayHello(
fruit: "apple" | "orange",
fruitOptions: any
) {
switch (fruit) {
case "apple":
console.log(apple has ${fruitOptions} iron)
break
case "orange":
console.l
...```
!ts
type AppleOptions = {
iron: number
}
type OrangeOptions = {
vitaminC: number
}
function sayHello(fruit: 'apple' | 'orange', fruitOptions: any){
switch(fruit){
case 'apple':
console.log(`apple has ${fruitOptions} iron`)
break
case 'orange':
console.log(`Orange has ${fruitOptions.vitaminC} vitamin C`)
}
}```
is fruitOptions supposed to be the number or an object?
yes its a mistake, im unable to edit
type AppleOptions = {
iron: number
}
type OrangeOptions = {
vitaminC: number
}
function sayHello(fruit: 'apple' | 'orange', fruitOptions: any){
switch(fruit){
case 'apple':
console.log(`apple has ${fruitOptions.iron} iron`)
break
case 'orange':
console.log(`Orange has ${fruitOptions.vitaminC} vitamin C`)
}
}
this is kinda hard to do safely
you could use a generic function, but then the generic could be a union.
it'd be easier if you could form the arguments as a discriminated union
earlier I tried using something like
sayHello(...args: ['apple' , {iron: number}] | ['orange' , {vitaminC: number}]){}
but that didn't work for some reason
oh, right, that is a discriminated union.
Do it with spread parameters + union.
Preview:```ts
type AppleOptions = {
iron: number
}
type OrangeOptions = {
vitaminC: number
}
function sayHello(
...[fruit, options]:
| [fruit: "apple", options: AppleOptions]
| [fruit: "orange", options: OrangeOptions]
) {
swit
...```
you need commas between elements, not unions
this worked, tysm 😽
!close
!open
ok it works, but is there a better way, thing is im building a library that has to be used by others, and the error looks like this
if there was a better way using generic please help
what about an object?
hmmmm yeah but this what my boss wants
and i like I would also like it this way, first param to be a string and 2nd depending on the first arg
possible to do something like this?
async dial<ChainType extends ChainNetwork>(
chain: ChainType,
options: ChainType === 'moi' ? MoiOptions : EthereumOptions,
) {}
type ChainNetwork = 'moi' | 'ethereum';
conditionals in signatures are almost never what you need
I mean I will be adding the runtime check also manually
also, you could pass ChainNetwork and then pass "moi" and EtheriumOptions
its just for types
I didn't get this, the 2nd part
You can use generic for this, but inside the implementation it would get very ugly.
DU is generally the way to go.
pls show how with the fruit example
pl0x
😽
Preview:```ts
type AppleOptions = {
iron: number
}
type OrangeOptions = {
vitaminC: number
}
type ParameterMap = {
apple: AppleOptions
orange: OrangeOptions
}
function sayHello<T extends keyof ParameterMap>(
fruit: T,
options: P
...```
You have no way to fix the error inside your implemenetation and just have to cast.
And it's also unsafe, people can do:
sayHello<'apple' | 'orange'>('apple', { vitaminC: 42 })
And it will explode at runtime.
I think I know what to do
first param is string
I can make an object with those strings as keys
and do some keyof magic with generics
ohhh lol