Function matchSchema

  • Given a Schema, returns a Type Guard that checks that the given value is an object implementing at least all the entries of the Schema.

    The difference between this Type Guard and matchExactSchema is that this Type Guard will allow additional entries that are not specified in the Schema. This is usually intended behavior because banning unknown keys would mean you have to implement a Type Guard for every value, if you don't care about them.

    Example

    import { matchSchema,  match } from 'type-guard-helpers'

    const test = {} as unknown
    const isFoo = match('foo');
    const isBar = match('bar');
    const isFooObject = matchSchema({
    foo: isFoo,
    isBar: isBar
    })

    if(isFooObject(test)){
    test; // { readonly foo: "foo"; readonly bar: "bar"; }
    }

    Type Parameters

    Parameters

    • schema: Schema

    Returns MatchSchemaFn<Schema>

Generated using TypeDoc