function identity<T>(arg: T): T {
return arg;
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
ngOnInit() {
let output = identity<string>('mystring');
console.log(output);
}
}
interface GenericIdentityFn {
<T>
}
identityFn<T>(arg: T): T {
return arg;
}
ngOnInit() {
let myIdentity: GenericIdentityFn = this.identityFn;
console.log(myIdentity('泛型接口'));
}
// 泛型接口参数
interface GenericIdentityParam<T> {
(arg: T): T;
}
// 泛型接口参数
identityParam<T>(arg: T): T {
return arg;
}
ngOnInit() {
let identityParamslist: GenericIdentityParam<number> = identity;
console.log(identityParamslist(100));