Skip to content

Maximum call stack size exceeded from getResolvedBaseConstraint #14940

@aaronbeall

Description

@aaronbeall

TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)

Code

I'm probably doing something stupid here but I can't quite wrap my mind around it and the compiler just crashes:

type ClassName<S> = keyof S;
type ClassNameMap<S> = { [K in keyof S]?: boolean }
type ClassNameObjectMap<S> = object & ClassNameMap<S>; // protects against passing non-objects, but breaks index lookups 
type ClassNameArg<S> = ClassName<S> | ClassNameObjectMap<S>;

export function css<S extends { [K in keyof S]: string }>(styles: S, ...classNames: ClassNameArg<S>[]): string {
  const args = classNames.map(arg => {
    if (arg == null) {
      return null;
    }
    if (typeof arg == "string") {
      return styles[arg];
    }
    if (typeof arg == "object") {
      return Object.keys(arg).reduce<ClassNameObject>((obj: ClassNameObject, key: keyof S) => {
        const exportedClassName = styles[key];
        obj[exportedClassName] = (arg as ClassNameMap<S>)[key]; 
        return obj;
      }, {});
    }
  });

  // return classnamer(...args);
  return "";
}

If I comment out the whole function body the errors go away.

Expected behavior:

No error, type S constrained to objects with only string properties and index lookups don't break.

Actual behavior:

Compiler fails with:

Process: Error processing message: Maximum call stack size exceeded RangeError: Maximum call stack size exceeded
    at isSimpleTypeRelatedTo (evalmachine.<anonymous>:28208:39)
    at isRelatedTo (evalmachine.<anonymous>:28354:21)
    at isRelatedTo (evalmachine.<anonymous>:28413:38)
    at isRelatedTo (evalmachine.<anonymous>:28413:38)
    at isRelatedTo (evalmachine.<anonymous>:28413:38)
    at isRelatedTo (evalmachine.<anonymous>:28413:38)
    at isRelatedTo (evalmachine.<anonymous>:28413:38)
    at isRelatedTo (evalmachine.<anonymous>:28413:38)
    at isRelatedTo (evalmachine.<anonymous>:28413:38)
    at isRelatedTo (evalmachine.<anonymous>:28413:38)

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions