#Why is filtering on an array inferring the type?

25 messages · Page 1 of 1 (latest)

rancid quail
#

I've got this short snippet

    const value = current
        .filter((x) => x.key === key);
    return value;

The current variable is Target[] | Bonus[]. But value is being typed as just Bonus[]. Both Bonus and Target are declared classes in my .d.ts file, and they both have a key property. I'm not sure why this is losing the either/or type here.

(p.s. this is javascript using jsdoc comments for typing, hence why there's a specific .d.ts file)

ruby galleon
#

Is key the same type for their defintions? Do you have an example I can run in the sandbox?

dapper tusk
#

unions can only look up methods on one of their members

#

you might have to do

const current2: (Target | Bonus)[] = current
rancid quail
# ruby galleon Is `key` the same type for their defintions? Do you have an example I can run in...

Yes, key is the same type for both. I've also tried putting key in an interface that both Target and Bonus implement but that didn't change anything.

Even though the site is complaining about the type declarations in the js file, they still work within the web editor (in my real code they're in a .d.ts file but I didn't see a way to do that in this online editor). The function at the bottom that displays the issue I'm having

cold dragonBOT
#
claudekennilol#0

Preview:```ts
/** BEGIN TYPE DECLARATIONS */

declare type TargetBonusValue = Array<
    string | number | object | string[] | number[] | object[]
>;

declare class Target {
    key: string;
    value: TargetBonusValue;
    join: 'and' | 'or';

...```

rancid quail
ruby galleon
#

For the difference in execution, try the ts config dropdown. language was set to javascript on mine

dapper tusk
rancid quail
ruby galleon
#

ah okay, I forgot abouit the comment the other day

rancid quail
# dapper tusk .d.ts is "typescript definitions"

I saw that there's a .d.ts tab in the top right, but clicking it only shows the inferred types from the editor - I don't see any way to actually edit it. Either way, the link as is shows what my issue is if you look at the code under the type definitions

dapper tusk
rancid quail
#

i.e. this here. Despite the editor complaining that the type are declared in a js file, those types are still being picked up

dapper tusk
rancid quail
# dapper tusk

As far as I can tell, I can have a single .ts file (top option for TypeScript), a single .d.ts file (highlighted in your pic), or a single js file (third option that I've chosen). I don't see any way to have both a .js file and a .d.ts file

ruby galleon
#

this is just a typescript moment
Is moment a typescript term? or are you saying something is evaluated at the transpile time, ie: that distinguisher isn't accessible, or something else?

rancid quail
#

I think it's an idiom like "she's just having one of her moments" i.e. it's just something that you've gotta deal with (which I'm fine if that's the answer, but I wanted to provide a full example)

ruby galleon
#

Oh. I'm new here, so I'm not always sure whether something is a new keyword, or a regular word

cold dragonBOT
#
d.n_n.b#0

Preview:```ts
...
const _getSetTargetBonusByKey = <
BonusOrTarget extends "bonuses" | "targets"

(
item: BaseDocument,
setIndex: number,
type: BonusOrTarget,
key: string
): SetTargetBonus[BonusOrTarget][number][] => {
const sets = _getSets(item)
const current: SetTargetBonus[BonusOrTarget][number][] =
sets[setIndex]?.[type]
const value = current.filter(x => x.key === key)
return value
}
...```

dapper tusk
#

yeah the issue here is that TS doesn't really work with generic type parameters in function bodies

#

the only pattern that really works is T[K]

dapper tusk
# cold dragon

to be fair, in this case you can refactor it into something that uses T[K]

dapper tusk