Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
57 lines (46 sloc) 1.12 KB
interface userInfo {
list?:ReadonlyArray<number>,
readonly type?: any, // 只读 readonly
mobile: number, // 必选
username?: string, // 可选
// [propName: string]: any // 不定义这个,就不能增加其他参数
}
interface peple extends userInfo {
readonly p: string
}
function login(userinfo: userInfo) {
// userinfo.list.push(4); // error 只读
userinfo.username = 'yaim';
// userinfo.type = 'type2'; // 接口中定义了只读,不能修改
console.log(userinfo);
}
function person(p: peple) {
// userinfo.list.push(4); // error 只读
p.username = 'yaim';
// userinfo.type = 'type2'; // 接口中定义了只读,不能修改
console.log(p);
}
// ok
// login({
// mobile: 123456789
// });
login({
list: [1, 2, 3],
type: 'type',
mobile: 123456789,
username: 'yaimeet',
// p: 'error'
// password: 'test'
});
person({
list: [1, 2, 3],
type: 'type',
mobile: 123456789,
username: 'yaimeet',
// password: 'test',
p: 'abc'
});
// error 接口定义了该mobile属性为number
// login({
// mobile: 'test'
// });
You can’t perform that action at this time.