Skip to content

emitDecoratorMetadata combined with template literals throws exception and exits #38428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
culli opened this issue May 8, 2020 · 0 comments · Fixed by #39481
Closed

emitDecoratorMetadata combined with template literals throws exception and exits #38428

culli opened this issue May 8, 2020 · 0 comments · Fixed by #39481
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@culli
Copy link

culli commented May 8, 2020

This is an edge case, but my team for various reasons decided to use template literals everywhere instead of strings. In almost all cases this seems to not have any drawbacks, except this case I'm reporting and one other edge case I may report separately. Maybe we shouldn't be using template literals in this spot, but the transpilation should probably not crash regardless. In our scenario emitDecoratorMetadata is required (for TypeORM) and that option is required to reproduce the bug.

TypeScript Version:
3.8.3
typescript@next 4.0.0-dev.20200508

Search Terms:
decorator
quotes
backtick

Code

import "reflect-metadata";

const formatMetadataKey = Symbol("format");

function format(formatString: string) {
  return Reflect.metadata(formatMetadataKey, formatString);
}

function getFormat(target: any, propertyKey: string) {
  return Reflect.getMetadata(formatMetadataKey, target, propertyKey);
}


class Greeter {
  @format("Hello, %s")
  greeting: `boss` | `employee` = `employee`;  //template literals on this line cause the issue

  constructor(message: 'boss' | 'employee') {
    this.greeting = message;
  }
  greet() {
    const formatString = getFormat(this, "greeting");
    return formatString.replace("%s", this.greeting);
  }
}

const g = new Greeter('boss')
g.greet()

Expected behavior:
Compiles normally.

Actual behavior:
Throws exception:

/Users/jcullison/.config/yarn/global/node_modules/typescript/lib/tsc.js:81040
                throw e;
                ^
Error: Debug Failure. Unexpected node.
Node 14 was unexpected.
    at serializeTypeNode (/Users/jcullison/.config/yarn/global/node_modules/typescript/lib/tsc.js:64342:45)
    at serializeTypeList (/Users/jcullison/.config/yarn/global/node_modules/typescript/lib/tsc.js:64391:44)
    at serializeTypeNode (/Users/jcullison/.config/yarn/global/node_modules/typescript/lib/tsc.js:64356:28)
    at serializeTypeOfNode (/Users/jcullison/.config/yarn/global/node_modules/typescript/lib/tsc.js:64249:28)
    at addOldTypeMetadata (/Users/jcullison/.config/yarn/global/node_modules/typescript/lib/tsc.js:64191:92)
    at addTypeMetadata (/Users/jcullison/.config/yarn/global/node_modules/typescript/lib/tsc.js:64185:17)
    at transformAllDecoratorsOfDeclaration (/Users/jcullison/.config/yarn/global/node_modules/typescript/lib/tsc.js:64104:13)
    at generateClassElementDecorationExpression (/Users/jcullison/.config/yarn/global/node_modules/typescript/lib/tsc.js:64129:40)
    at generateClassElementDecorationExpressions (/Users/jcullison/.config/yarn/global/node_modules/typescript/lib/tsc.js:64115:34)
    at addClassElementDecorationStatements (/Users/jcullison/.config/yarn/global/node_modules/typescript/lib/tsc.js:64108:44)

Playground Link: Provided

Console output:
Error: Debug Failure. Unexpected node.
Node NoSubstitutionTemplateLiteral was unexpected.

Related Issues:
(similar idea) #37276

Example cli:*
tsc --outdir ./build --target ES2019 --experimentalDecorators -emitDecoratorMetadata ./test-param-decorator.ts

@culli culli changed the title emitDecoratorMetadata combined with template literals throws except and exits emitDecoratorMetadata combined with template literals throws exception and exits May 9, 2020
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 5, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.0 milestone Jun 5, 2020
@weswigham weswigham added the Fix Available A PR has been opened for this issue label Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
3 participants