Skip to content

TC39 Accessor Decorators unable to use declareΒ #61418

@NullVoxPopuli

Description

@NullVoxPopuli

πŸ”Ž Search Terms

decorator, declare, accessor

πŸ•— Version & Regression Information

afaict, this never worked? (it should tho)

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgLIE8DK0BuwkDeAvgNwBQZEAHgA4D2UYyCANnAM7vIBiddyBMsmQABdrnwpESTg2QATCKzhQUMPgC40WCUnJEKAekPJ2NJchMAVAMIBmAJzIAIkoZwwDMjACuIBGDAdCCmuhAAPABqcCw+EAB8ABRCyGAqAOYQYFo2bJwAgggy7AyuCO6eUFYZWeF+ANYgdADuIAA0yNGxCR0p5eDU2ci5HOyFxaVuUB4MNsGQVGBkAJQ5eWNFELJQZRUMAEpbPixgdSCNLe2dMXHx5GTGyMAAtjQsEM8Q4B5BIYlg6HMCjoWxAAHImM8PJAoB1qIgwCx0MtvH4Ar9QlA8EgojcEokAHREjLsLQNJqtADaAF1VshyZcBClVGAfFAQgAGZAcennCkhHldOL6ChAA

πŸ’» Code

interface MyService{};

export class Foo {
  @service accessor declare foo: MyService;
}

// spec / TC39 Decorator
function service<Value>(
  target: ClassAccessorDecoratorTarget<unknown, Value>, 
  context: ClassAccessorDecoratorContext
): ClassAccessorDecoratorResult<unknown, Value>;

// implementation (type doesn't matter, exactly)
function service<Value>(...args: unknown[]): unknown {
  return 0 as unknown as Value;
}

πŸ™ Actual behavior

error here:

export class Foo {
   @service accessor declare foo: MyService;
// ^
// \- Decorators are not valid here (1206)
}

πŸ™‚ Expected behavior

no error - decorators can define values on access, lazily, so declare should be allowed since declare means "TS doesn't normally know this can have a value, but I'm declaring that 'something' that TS can't see is setting that value".

where as, what does "work" is ! -- which is often linted against, and colloquially means approx "idgaf, I'm right" -- which is an appropriate thing to lint against.

Additional information about the issue

No response

Metadata

Metadata

Assignees

Labels

Working as IntendedThe behavior described is the intended behavior; this is not a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions