Answer the question
In order to leave comments, you need to log in
Why is there no error when assigning an array from other interfaces?
Hello. Completed a beginner course on TS and started implementing Vue 3 into a project .
There is a REST API function that pulls the view data:{ meta, volumes }
export const getVolumes = (
params: RequestMeta,
): Promise<{ meta: RequestMeta, volumes: Volume[] }> => request({
url: '/volumes/',
params,
});
data() {
return {
volumes: [] as Array<Volume>,
};
}
methods: {
fetchVolumes() {
getVolumes().then((data) => {
this.volumes = data.volumes;
});
}
}
Answer the question
In order to leave comments, you need to log in
In ts, structural typing. This can happen if the Snapshot is structurally within the Volume type.
Those. This is how the types are interchangeable:
interface Volume {
id: number;
volume?: string;
}
interface Snapshot {
id: number;
snapshot?: string;
}
And so no:interface Volume {
id: number;
volume: string;
}
interface Snapshot {
id: number;
snapshot: string;
}
interface Volume {
id?: number;
}
interface Snapshot {
id?: string;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question