const deviceType = [
{
smartphoneName: 'Precision 5530',
deviceType: 'smartphone',
},
{
smartphoneName: 'MS-7A34',
deviceType: 'computer',
},
{
smartphoneName: 'MS-7A34',
deviceType: 'computer',
},
] as Array<{
smartphoneName: string;
deviceType: DeviceType;
}>interface IDeviceResponseItem {
smartphoneName: string;
deviceType: DeviceType;
}
function isDeviceResponseItem(item: unknown): item is IDeviceResponseItem {
if (!item)
return false;
const { smartphoneName, deviceType } = item as IDeviceResponseItem;
if (typeof smartphoneName === 'string' && deviceType in DeviceType)
return true;
return false;
}
function getResponse(): IDeviceResponseItem[] {
// ответ от сервера неизвестного типа
const json: unknown = [
{
smartphoneName: 'Precision 5530',
deviceType: 'smartphone',
},
{
smartphoneName: 'MS-7A34',
deviceType: 'computer',
},
{
smartphoneName: 'MS-7A34',
deviceType: 'computer',
},
];
// проверка что ответ именно тот который нужен
if(!Array.isArray(json) || !json.every(isDeviceResponseItem))
throw new Error('[wrong response]');
return json;
}
const deviceType = getResponse();
Array<{
smartphoneName: string;
deviceType: DeviceType;
}>any.
export enum DeviceType ...
import { DeviceType } from ...Мне бы как-то только строку получать
Test test</span></div><span class="fonth5_bold mt3">Sep ">01, 2022</span> то ничего плохого, кроме "некрасивости".
const repoMap = {
"api-repo-1": ApiRepo1,
"api-repo-2": ApiRepo2
} as const;any просто BaseRepository, т.к. оба варианта должны оный имплементировать. Но я так понял у вас разница какая-то есть...
@input - метод, который делает "update" commit, мутация же сама внутри себя находит конкретный item по id и обновляет значение конкретного поля.mutations: {
updateItemThumb(state, { id, thumb }) {
const item = state.allItems.find(item => id === item.id);
if (item) item.thumb = thumb;
}
}data: {
changes: {}
}:value="(changes[item.id] || item).title" @input="updateItemTitle(item.id, $event.target.value)"methods: {
updateItemTitle(id, title) {
if (!this.chages[id]) this.$set(this.changes, id, { title });
else this.chages[id].title = title;
},
saveChanges() {
const allItems = this.$store.state.allItems.map(item => {
if (item.id in this.changes)
return {...item, ...this.changes[id]};
return item;
});
this.$store.commit('allItems', allItems);
}
}
item.thumb = $event.target.files[0].name, только через коммит.
<AllFriends/> куда либо, то "Отрабатало!" должно появляться в консоли ровно один раз. ts-ignore в коде быть не должно. Решение "проблем" с типами, особенно в таких стандартных юзкейсах как у тебя, нагугливаются на раз два.
Т.е. твоя задача в данном случае указать нужный тип на этапе компиляции + убедиться что ты не наврал на этапе исполнения.