Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

问一个接口的问题 #156

Open
bigGoodMan opened this issue Dec 30, 2019 · 1 comment
Open

问一个接口的问题 #156

bigGoodMan opened this issue Dec 30, 2019 · 1 comment

Comments

@bigGoodMan
Copy link

bigGoodMan commented Dec 30, 2019

 interface ObjINF1 {
    [key: string]: string | number | undefined;
}
interface ObjINF2 {
    id: number;
    title: string;
}
function a1(objArr: ObjINF1[]) {
    
}
function a2(objArr: ObjINF2[]) {
// Argument of type 'ObjINF2[]' is not assignable to parameter of type 'ObjINF1[]'.Type 'ObjINF2' is not assignable to type 'ObjINF1'. Index signature is missing in type 'ObjINF2'.
    a1(objArr) 
}

function a3(objArr: {
    id: number;
    title: string;
}[]) {
    a1(objArr) 
}

为什么a2报错了 a3 反而正确

@only4s44u
Copy link

需要显式声明 interface ObjINF2 extends ObjINF1 {...} 才能保证 ObjINF2 类型可以赋值给 ObjINF1 类型,a3 是用字面量定义的所以没问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants