Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何将对象中的类序列化为字符串_Node.js_Typescript - Fatal编程技术网

Node.js 如何将对象中的类序列化为字符串

Node.js 如何将对象中的类序列化为字符串,node.js,typescript,Node.js,Typescript,我有以下对象icoreflestat,在服务器端,URI将是使用创建的类。如何仅将URI递归转换为字符串 export interface ICoreFileStat extends ICoreBaseStat { children?: ICoreFileStat[]; } export interface ICoreBaseStat { resource: URI | string; name?: string; } 我所期望的是,上面对象中的URI将被转换为stringlike

我有以下对象
icoreflestat
,在服务器端,URI将是使用创建的类。如何仅将
URI
递归转换为字符串

export interface ICoreFileStat extends ICoreBaseStat {
  children?: ICoreFileStat[];
}
export interface ICoreBaseStat {
  resource: URI | string;
  name?: string;
}
我所期望的是,上面对象中的URI将被转换为
string
like
(file:///index.js)
它将是一个简单的对象,如下所示

const data = {
children: {
   resource: "///file://tmp"
   children: {
      resource: "///file:/index.js"
    }
}

这是我想出的解决办法。但我也希望看到其他解决方案

const serialize = (stat: IFileStat) => Object.keys(stat).reduce((result, name) => {
    // console.log(result);
    if (name === 'resource') {
        return result = { ...stat, resource: stat[name].toString() };
    } else if (name === 'children') {
        return { ...result, ...{ children: stat[name].map(child => serialize(child as any)) } }
    }
    else {
        return result
    }
}, {});