function returnProp(returnProp: string | number): string | number {
return returnProp;
}
But will yield an error cause TS doesn't know if it's a string or number
// ❌ This will yield an error
// > Operator '+' cannot be applied to types '4' and 'string | number'.
const newNumber = shouldBeNumber + 4;
function returnProp(returnProp: number): number;
function returnProp(returnProp: string): string;
// While this seems repetitive, TS requires it.
// Otherwise, it will complain:
// This overload signature is not compatible with its implementation signature.
function returnProp(returnProp: string | number): string | number {
return returnProp;
}
function returnSelf<T>(returnProp: T): T {
return returnProp;
}
// This is allowed now
interface LogTheValueReturnType<originalT> {
loggedValue: string;
original: originalT;
err: Error | undefined;
}
interface TimestampReturn<T> {
isPast: boolean;
isFuture: boolean;
obj: T
}
const checkTimeStamp = <T extends {time: Date}>(obj: T): TimestampReturn<T> => {
let returnVal: TimestampReturn<T> = {
isPast: false,
isFuture: false,
obj
}
if (obj.time < Date.now()) {
returnVal.isPast = true;
} else {
returnVal.isFuture = true;
}
return returnVal;
}
@Component({
selector: 'my-app',
template: `
<ng-template #templ>
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
</ul>
</ng-template>
<div #viewContainerRef class="testing">
</div>
`
})
export class AppComponent implements OnInit {
@ViewChild('viewContainerRef', {read: ViewContainerRef, static: true}) viewContainerRef;
@ViewChild('templ', {read: TemplateRef, static: true}) templ;
ngOnInit() {
this.viewContainerRef.createEmbeddedView(this.templ);
}
}
import { Component, ViewContainerRef, OnInit, AfterViewInit, ContentChild, ViewChild, TemplateRef , EmbeddedViewRef} from '@angular/core';
@Component({
selector: 'my-app',
template: `
<ng-template #templ let-i>
<li>List Item </li>
<li>List Item </li>
</ng-template>
<ul>
<div #viewContainerRef></div>
</ul>
`
})
export class AppComponent implements OnInit {
@ViewChild('viewContainerRef', {read: ViewContainerRef, static: true}) viewContainerRef;
@ViewChild('templ', {read: TemplateRef, static: true}) templ;
ngOnInit() {
const embeddRef3: EmbeddedViewRef<any> = this.viewContainerRef.createEmbeddedView(this.templ, {$implicit: 3});
const embeddRef1: EmbeddedViewRef<any> = this.viewContainerRef.createEmbeddedView(this.templ, {$implicit: 1});
}
}
@Directive({
selector: '[renderTheTemplate]'
})
export class RenderTheTemplateDirective implements OnInit {
constructor (private parentViewRef: ViewContainerRef, private templToRender: TemplateRef<any>) {}
ngOnInit(): void {
this.parentViewRef.createEmbeddedView(this.templToRender);
}
}
@Directive({
selector: '[renderThisIf]'
})
export class RenderThisIfDirective {
constructor (private templ: TemplateRef<any>,
private parentViewRef: ViewContainerRef) {
}
private _val: TemplateRef<any>;
@Input() set renderThisIf(val: TemplateRef<any>) {
this._val = val;
this.update();
}
update(): void {
if (this._val) {
this.parentViewRef.createEmbeddedView(this.templ);
} else {
this.parentViewRef.clear();
}
}
}