0
type ParameterInfo = {
    required?: true;
    pattern?: RegExp;
    // other known keys
    [otherKeys: string]: ParameterInfo;
}

So I can have a ParameterInfo with 0-many nested ParameterInfos under it, e.g.:

{
    user: { // this is a ParameterInfo
        required: true,
        username: { // I'm a ParameterInfo, too
            required: true,
            pattern: /.../,
        },
        password: { // I'm a ParameterInfo, too
            required: true,
        },
    },
}

Using the type definition at the top, all of the known keys (e.g. required, pattern) are highlighted with the error:

Property 'required' of type 'true' is not assignable to string index type 'ParameterInfo'. ts(2411)

Josh M.
  • 26,437
  • 24
  • 119
  • 200
  • 1
    Unfortunately there's no simple way to do this in TypeScript; see the other answer for possibilities. By far the best way for you to deal with this is to push the recursive part down one level like [this](https://tsplay.dev/DmMblW), requiring refactoring of your runtime code. Otherwise it's all going to be workarounds like the intersection. – jcalz Jan 18 '21 at 03:05
  • I originally had the child params under a `parameters` property as you suggest. It just got so verbose traversing the tree. :( – Josh M. Jan 18 '21 at 03:18

0 Answers0