Using index signatures with methods - Typescript 3.5

后端 未结 2 2004
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-28 04:54
interface Keys {
  [key: string]: any
}

const obj: Keys = {
  trimDescription(text: string, length: number): string {
    return text.length > length ? text.substrin         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-28 05:31

    interface Keys {
        [key: string]: (text: string, length: number) => string;
    }
    
    const obj: Keys = {
        trimDescription(text: string, length: number): string {
            return text.length > length ? text.substring(0, length - 3) + '...' : text
        }
    }
    
    Object.keys(obj).forEach(key => {
        console.log(obj[key])
    })
    

提交回复
热议问题