syamskoy
@syamskoy

Почему возникает такая ошибка?

Vue3, Vite, TS.

Есть вот такой код:

const props = defineProps({
  comment:   comment: { type: Comment, required: true },
})

const hasReplies = computed(() => {
  return props.comment.replies.length > 0
})


Если Comment сделать интерфейсом:
export interface Comment {
    id: number;
    author: string;
    text: string;
    replies: Comment[]
}


То получаю ошибку, что свойство replies в comment не найдено:
TS2339: Property 'replies' does not exist on type 'Comment'.

Если интерфейс заменить на class, то ошибки нет:

export class Comment {
    id: number;
    author: string;
    text: string;
    replies: Comment[]

    constructor(json: any) {
        this.id = json.id;
        this.author = json.author;
        this.text = json.text;
        this.replies = json.replies;
    }
}


Что не так с интерфейсом?
  • Вопрос задан
  • 57 просмотров
Решения вопроса 1
Djaler
@Djaler
Сеньор-помидор
comment: {
    type: Object as PropType<Comment>,
    required: true
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы