Home > Software engineering >  How to print the FULL output, not collapsed output, of type error messages and definitions in TypeSc
How to print the FULL output, not collapsed output, of type error messages and definitions in TypeSc

Time:12-29

Here is an example of viewing a type definition which is ellipsed:

const yx: undefined[] | string[] | number[] | ({
    partial: true;
    children: (... | {
        complete: boolean;
        partial: false;
        dive: boolean;
        like: AST.Term;
        name: string;
        nest: ...[];
    } | {
        partial: true;
        children: string[];
        like: AST.String;
    } | {
        ...;
    })[];
    like: AST.Term;
} | ... 6 more ... | {
    ...;
})[] | ... 20 more ... | ({
    partial: true;
    children: (... | {
        complete: boolean;
        partial: false;
        dive: boolean;
        like: AST.Term;
        name: string;
        nest: ...[];
    } | {
        partial: true;
        children: string[];
        like: AST.String;
    } | {
        ...;
    })[];
    like: AST.Term;
} | ... 2 more ... | {
    ...;
})[]

Notice the ... 20 more ... and such. They are not clickable/expandable so I cannot see what is in there to debug. How can I better debug a type definition like that? I used ExpandRecursively like so to see it:

const yx: ExpandRecursively<X['children']>


// expands object types one level deep
export type Expand<T> = T extends infer O
  ? { [K in keyof O]: O[K] }
  : never

// expands object types recursively
export type ExpandRecursively<T> = T extends object
  ? T extends infer O
    ? { [K in keyof O]: ExpandRecursively<O[K]> }
    : never
  : T

Likewise, how can I get the error message to expand all its output in TypeScript?

Argument of type 'X' is not assignable to parameter of type 'X["children"]'.
  Type 'ASTPartialType<AST>' is not assignable to type 'X["children"]'.
    Type 'ASTTemplate_PartialType' is not assignable to type 'X["children"]'.
      Type 'ASTTemplate_PartialType' is not assignable to type '(ASTInput_PartialType | ASTTerm_Type)[] | (ASTTerm_Type | ASTClassReference_Type)[] | (ASTTerm_Type | ASTString_Type)[] | ... 21 more ... | number[]'.
        Type 'X' is not assignable to type '(ASTInput_PartialType | ASTTerm_Type)[] | (ASTTerm_Type | ASTClassReference_Type)[] | (ASTTerm_Type | ASTString_Type)[] | ... 21 more ... | number[]'.
          Type 'ASTPartialType<AST>' is not assignable to type '(ASTInput_PartialType | ASTTerm_Type)[] | (ASTTerm_Type | ASTClassReference_Type)[] | (ASTTerm_Type | ASTString_Type)[] | ... 21 more ... | number[]'.
            Type 'ASTTemplate_PartialType' is not assignable to type '(ASTInput_PartialType | ASTTerm_Type)[] | (ASTTerm_Type | ASTClassReference_Type)[] | (ASTTerm_Type | ASTString_Type)[] | ... 21 more ... | number[]'.
              Type 'X' is not assignable to type 'number[]'.
                Type 'ASTPartialType<AST>' is not assignable to type 'number[]'.
                  Type 'ASTTemplate_PartialType' is missing the following properties from type 'number[]': length, pop, push, concat, and 29 more.

In there it says 29 more, ... 21 more ... a few times, etc.. How can I have it show every single thing so I can tell what's in the union type?

CodePudding user response:

For errors you can set to true the noErrorTruncation flag. For general inferred types there are some hacks listed here: https://github.com/microsoft/TypeScript/issues/26238

  • Related