I'm newbie to the angular 2 / typescript. I'm working around around interfaces in typescript. I got the point as by using this we can check our type in compile or dev time itself. I had created a Interface ts file as like below with 2 properties and 1 function :
export interface EmpEnities{
empname : string;
deptname :string;
validation();
}
My Component :
import { Component } from '@angular/core';
import {EmpEnities} from './IEmloyee'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
Employees : EmpEnities[] = [
{empname:'sreekanth',deptname:'xyz',validation(){return 'a'}},
{empname:'sreekanth',deptname:'xyz',validation(){return false}},
{empname:'sreekanth',deptname:'xyz',validation(){return true}}
]
}
I had perfectly got compile time error if any of the three members(2 properties + 1function) not used. Now my criteria is How can I make sure to get error, if my "Validation()" is not returning a bool value. If you observe in the array of object, the first validate() is returning string and remaining are returning boolean.
Can I specify the return type of the Function declared in Interface ?
答案 0 :(得分:0)
尝试一下
export interface EmpEnities{
empname : string;
deptname :string;
validation: () => boolean;
}
export class AppComponent {
title = 'app';
Employees : EmpEnities[] = [
{empname:'sreekanth',deptname:'xyz',validation: () => {return 'a' === 'a'}},
{empname:'sreekanth',deptname:'xyz',validation: () => {return false}},
{empname:'sreekanth',deptname:'xyz',validation: () => true}
]
}