i have this interface
interface Product {
id: string
name: string
}
and the one that extends it
interface AdminProduct extends Product {
isVisible: boolean
}
then i have this code
const adminProducts = (await queryByCollection('product')) as AdminProduct[]
const products = adminProducts.filter((product) => {
if (!product.isVisible) {
return
}
}).map((product)=> product as Product) as Product[]
return products
even though i define the return type of product when i map adminProducts array it still has the key 'isVisible' in it
how to convert an object to smaller interface and delete all redundant keys?
CodePudding user response:
Typescript only presents in build time and after transpile, tsc
(typescript compiler) will remove all typescript types and only javascript remains.
So can't expect to manipulate the data by TS.
You have to do it in the javascript area:
adminProducts
.filter((adminProduct) => adminProducts.isVisible)
.map(({isVisible, ...product}) => product)
With this code, you will remove isVisible
from your list.