Closed m-rutter closed 2 years ago
my tsconfig:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"downlevelIteration": true
},
"include": ["src"]
}
Sorry for the multi-year delay; I can't imagine it's still relevant but if it is, or it is to someone else, the issue is simply that you were trying to fill in the ReadonlyArray<{ __typename?: "Baz"; value: number; }>;
with a normal array and the error is saying that you can't assign a mutable list to a read only list. Specifying that your list is not mutable would resolve this:
type Maybe<T> = null | T;
interface BazT {
__typename?: "Baz";
value: number;
}
interface Thing {
__typename?: "Thing";
foo?: Maybe<{
bar: {
__typename?: "Bar";
otherValuesThatICannotProvideDefaultsFor: {
value: string;
__typename?: "Boo";
};
bazs: ReadonlyArray<BazT>;
};
__typename?: "Bar";
}>;
}
declare const thing: Thing;
get("foo", valueOr({ bar: { bazs: [] as ReadonlyArray<BazT> } }), "bar", "bazs")(thing);
Hi, I really like the look of the library and I'm trying to compare it against things like
monocle-ts
.I'm struggling a bit figuring out how to deal with objects with nullable values. I'm dealing with things generated by graphql codegen, which tends to generate types full of nullable values.
Consider this interface for example which is very typical of what graphql codgen generates:
I think in the case of things like
monocle-ts
I would use stuff likeOptional
to getOption
s of the result of my equiliventget
andmod
functions. What would I do here? It might be nice to have some documentation examples of how to deal with nullable values. I've tried usingvalueOr
to provide default values, but I get various type errors: