How shall I use conform() to check if a runtype conforms to a type with optional properties?

1 day ago 1
ARTICLE AD BOX

I have set compiler option as recommended by the docs:

"verbatimModuleSyntax": true

But it turned out that my trial to check conformity of a runtype to an existing regular TypeScript interface does not work properly when there are optional properties. For example, the following does not produce a compiler error even though the types do not conform:

import * as rt from "runtypes"; export interface MemberChanges { added?: string[]; deleted?: string[]; x: number; } export const MemberChangesRt = rt.Object({ added: rt.Optional(rt.Array(rt.String)), bla: rt.Optional(rt.Array(rt.String)), x: rt.Number, }).conform<MemberChanges>();

It does not matter if the name in MemberChangeRt is correct for deleted. But, if I remove the optional (? in MemberChanges and corresponding rt.Optional in MemberChangesRt), the conformance check works properly. Have I made a mistake or is it just not meant for optional properties? The problem is the same if I use the suffix .optional() instead of the function rt.Optional().

Read Entire Article