Ng-packagr: [AoT] Partial type in service constructor not resolved

Created on 16 Jan 2019  路  3Comments  路  Source: ng-packagr/ng-packagr

Type of Issue

[ X ] Bug Report
[ ] Feature Request

Description

Inkection tokens for configuring some services should provide some values which will override default ones. That's what Partial type does - set each of class property as not required. Angular services works and builds well with this behavior, but libraries are not.

How To Reproduce

Here's a project built using default commands with updated to Angular 7.2 dependencies and Partial in library constructor. Clone it and run npm run build:lib. The error you will get:

BUILD ERROR
D:/Soft/OSPanel/node/angular/partial-error-setup/projects/test-lib/src/lib/test-lib.service.ts:10:1: Error encountered in metadata generated for exported symbol 'TestLibService':
D:/Soft/OSPanel/node/angular/partial-error-setup/projects/test-lib/src/lib/test-lib.service.ts:16:33: Metadata collected contains an error that will be reported at runtime: Could not resolve type Partial.
{"__symbolic":"error","message":"Could not resolve type","line":15,"character":32,"context":{"typeName":"Partial"}}

Expected Behaviour

Regular build without error.

Version Information

$ node_modules/.bin/ng-packagr --version
ng-packagr:            4.6.0
@angular/compiler:     7.2.0
rollup:                0.67.4
tsickle:               0.34.0
typescript:            3.2.2
node: 10.13.0
npm/yarn: 6.4.1
bug roadmap (high)

Most helpful comment

I'm facing this issue too.

I'm building out a library of components to be shared across multiple projects. In the past, these components were embedded in a particular project. But now, we want to share them via an npm package built through an angular cli library. The components take some optional overrides from outside.

export interface IndicatorConfig {
...
}

export const INDICATOR_CONFIG = new InjectionToken<Partial<IndicatorConfig>>('my-indicator.indicator-config');

@Injectable({
  providedIn: 'root'
})
export class MyIndicatorService {
  constructor(
    @Optional()
    @Inject(INDICATOR_CONFIG)
    private configOverrides: Partial<IndicatorConfig>,
  ) {}
...
}

I imagine this is a desirable use case. Is it intentionally not supported?

All 3 comments

Code:

export class User {
  public login: string;
  public password: string;
}

export const USER = new InjectionToken<Partial<User>>('user');
@Injectable({
  providedIn: 'root'
})
export class TestLibService {

  constructor(
    @Inject(USER) private user: Partial<User>
  ) { }
}

I'm facing this issue too.

I'm building out a library of components to be shared across multiple projects. In the past, these components were embedded in a particular project. But now, we want to share them via an npm package built through an angular cli library. The components take some optional overrides from outside.

export interface IndicatorConfig {
...
}

export const INDICATOR_CONFIG = new InjectionToken<Partial<IndicatorConfig>>('my-indicator.indicator-config');

@Injectable({
  providedIn: 'root'
})
export class MyIndicatorService {
  constructor(
    @Optional()
    @Inject(INDICATOR_CONFIG)
    private configOverrides: Partial<IndicatorConfig>,
  ) {}
...
}

I imagine this is a desirable use case. Is it intentionally not supported?

confirm the problem.

Right now we use type alias as workaround

// my-config.ts
export interface MyConfig {...}

// :tada: 
export type MyConfigPartial = Partial<MyConfig>

export const MY_CONFIG = new InjectionToken<MyConfigPartial>('MY_CONFIG');

// my.service.ts
import { MY_CONFIG, MyConfigPartial } from './my-config.ts';

@Injectable()
export class MyService {
  constructor(
    @Inject(MY_CONFIG) configPartial: MyConfigPartial, // !!!! accepted by AOT compilier
  ) {....}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

hei-pa picture hei-pa  路  3Comments

cnotarioArtica picture cnotarioArtica  路  4Comments

MalliswariKP picture MalliswariKP  路  4Comments

BioPhoton picture BioPhoton  路  3Comments

dareksob picture dareksob  路  4Comments