Open Kolobamanacas opened 2 months ago
The issue is that None is Option<never>
, so TypeScript is trying to be helpful by warning you of mismatched this
contexts.
To make this work you first need to widen the type of None to be an option. The example below hopefully indicates a suitable workaround and doesn't require any assertions:
type T = { test: true };
type Q = {
one: Option<T>;
two: Option<T>;
three: None;
};
const q: Q = null as any;
const qk: keyof Q = null as any;
q[qk].isNone(); // Error
// No need to make an assertion, as None is assignable to Option<T>
const x: Option<T> = q[qk];
x.isNone(); // No complaints
Thanks for the workaround, yet I don't quite follow how the example reproduces described case. In your example, typeof None
in type Q
is replaced with None
. This will break any actual map that will try to implement the Q
(or CurrencyClientMap
in my example).
// This will work.
type CurrencyClientMap = {
[Currency.USD]: typeof None
}
const currencyClientMap: CurrencyClientMap = {
[Currency.USD]: None
}
// This will not work.
type CurrencyClientMap = {
[Currency.USD]: None
}
const currencyClientMap: CurrencyClientMap = {
[Currency.USD]: None // Error: Type of computed property's value is 'Readonly<OptionType<never>>', which is not assignable to type 'None'.
}
Also, it seems like the issue is not in None
itself, but rather in the fact that A | B
logic is for some reason expected to be A & B
logic. In the original example, if one leaves any two options in Currency
enum and CurrencyClientMap
type respectively (like [Currency.EUR]: Option<GenericClientInterface>
and [Currency.UAH]: Option<SpecificClientInterface>
or [Currency.EUR]: Option<GenericClientInterface>
and [Currency.USD]: typeof None
), the issue remains.
Lastly, even if I fix this in my local project with any kind of workaround, it still seems like an issue with types in Oxide package and needs to be fixed.
To fully reproduce the issue, you could set up tiny Node.js project using the following code:
package.json
:tsconfig.json
:app.ts
:Actual result: If you set up a project the way described above, you'll notice that in line 35 of
app.ts
, there is a TypeScript error:This behavior seems wrong as all returned values are either options or None and all of them have
.isNone()
method.Expected result: The returned value of
Option<GenericClientInterface> | Option<SpecificClientInterface> | typeof None
type should not trigger any TypeScript errors.