Skip to content

Constructor param typed by a same-file interface is injected as a token (ReferenceError at runtime) #653

Open

Description

@Brooooooklyn

Summary

A constructor parameter typed by an interface or type alias that is declared in the same file is compiled as an injection token. The interface is erased, so creating the class throws ReferenceError: Foo is not defined. ngtsc treats the parameter as having no token.

Found while re-checking #623.

Reproduction

import {Injectable} from '@angular/core';

interface Foo { a: number }

@Injectable()
export class S {
  constructor(f: Foo) {}
}
ngtsc 22.2.3 oxc (main 5780233)
full i0.ɵɵinvalidFactory() new (t || S)(i0.ɵɵinject(Foo))
partial deps: "invalid" deps: [{ token: Foo }]
strictInjectionParameters NG2003 error no error

Cause

extract_param_token (component/decorator.rs, injectable/decorator.rs, and the copies in the directive, pipe and ng_module decorators) takes the name of any TSTypeReference and never checks whether that name is a type-only declaration in the file.

A fix can mark the dep invalid when the name is an interface or type declared in the same file, as is already done for type-only imports. An interface imported through a value import from another file can't be detected one file at a time.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions