Skip to content

Regression in advanced nested types in TS 3.2 #29168

Closed
@cyrilletuzi

Description

@cyrilletuzi

TypeScript Version: 3.2.2 & 3.3.0-dev.20181222
Problem started in TS 3.2 and is still there in next version.

Search Terms:
nested

Code

interface TestObject {
  type?: 'object';
  items: {
    [k: string]: TestGeneric;
  };
}

interface TestString {
  type: 'string';
}

type TestGeneric = (TestString | TestObject) & { [k: string]: any; };

const test: TestGeneric = {
  items: {
    hello: { type: 'string' },
    world: {
      items: {
        nested: { type: 'string' }
      }
    }
  }
};

Expected behavior:

Worked in TS 3.1.

Actual behavior:

Error since TS 3.2. The exact error varies from one variable to another, but it seems that when there is nesting, TS infers the first property correctly (let's say it's TestString), but fails on the next ones because it checks with the same inference as the first property (TestString), while it could be another one (TestObject).

It works if TestGeneric is just:

type TestGeneric = (TestString | TestObject);

without { [k: string]: any; }.

Use case:

cyrilletuzi/angular-async-local-storage#64

JSON schemas are structured this way. You can see an example in this lib, which uses JSON schemas for validation:

  • properties of a JSON schema can be another JSON Schema, so it can be nested ;
  • { [k: string]: any; } is added because the lib doesn't support all the features of the JSON schema standard, and I want the lib to allow standard JSON schemas without breaking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Contextual TypesThe issue relates to contextual types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions