I am trying to lookup the value of a key with fp-ts
. The key might not be present.
type Data = {
a?: number;
b?: string;
c?: { e?: string; w: number };
};
const e = R.lookup('b')({ a: 23, b: 'asdfasdf', c: { e: 'asdf', w: 23}} as Data)
I thought the type of e
inferred by Typescript would be Option<string>
but it is:
O.Option<string | number | {
e?: string | undefined;
w: number;
}>
This looks like all the possible types in Data
. Is this intended behaviour? And how shall I narrow my types to only the "potential" type of b
, so that I can continue a pipeline from an Option<string>
.
I tried the approach below, which marks e
as Option<string>
but then flags the entire object {a: 23...
as "not assignable to parameter of type 'Record<string, string>'"
const getB = R.lookup("b")
const e = getB<string>({ a: 234, b: "asdfasdf", c: { e: "asdf", w: 23 } } as Data);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/// Argument of type 'Data' is not assignable to parameter of type 'Record<string, string>'.
Property 'a' is incompatible with index signature.
Type 'number' is not assignable to type 'string'.