[ X ] Bug Report
[ ] Feature Request
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.
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"}}
Regular build without error.
$ 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
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
) {....}
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.
I imagine this is a desirable use case. Is it intentionally not supported?