Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/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
Typescript 从'中省略两次嵌套的属性;这';_Typescript_Typescript Typings_Typing - Fatal编程技术网

Typescript 从'中省略两次嵌套的属性;这';

Typescript 从'中省略两次嵌套的属性;这';,typescript,typescript-typings,typing,Typescript,Typescript Typings,Typing,我正在尝试添加一个从对象类型中删除内部属性的方法,由于无法更改复杂的基类,我遇到以下问题: 我编写了一个方法,该方法应该抛出一个强制类型转换属性(在本例中为name) 但是属性ceo.name仍然可以访问。 我相信这是因为在中添加一个不带ceo name属性的类型只需要最大的分母 i、 e{name:string}&{}={name:string} 有没有办法解决这个问题 class Company { ceo: {} addCeoNameType() { ret

我正在尝试添加一个从对象类型中删除内部属性的方法,由于无法更改复杂的基类,我遇到以下问题: 我编写了一个方法,该方法应该抛出一个强制类型转换属性(在本例中为name) 但是属性
ceo.name
仍然可以访问。 我相信这是因为在
中添加一个不带ceo name属性的类型只需要最大的分母
i、 e
{name:string}&{}={name:string}

有没有办法解决这个问题

class Company {
    ceo: {}
    addCeoNameType() {
        return this as this & {
            ceo: {
                name: string;
            }
        }
    }
    removeCeoNameType() {
        const ceoForType: this['ceo'] = null;
        return this as this & {
            ceo: Omit<typeof ceoForType, 'name'>;
        }
    }
}

class Foo {
    bar() {
        const catsInc = new Company();
        const catsIncWithCeoName = catsInc.addCeoNameType();
        catsIncWithCeoName.ceo.name;
        catsIncWithCeoName.removeCeoNameType().ceo.name;
    }
}
class公司{
首席执行官:{}
addCeoNameType(){
以this&{
首席执行官:{
名称:字符串;
}
}
}
removeCeoNameType(){
const ceoForType:this['ceo']=null;
以this&{
ceo:省略;
}
}
}
福班{
bar(){
const catsInc=新公司();
const catsIncWithCeoName=catsInc.addCeoNameType();
catsIncWithCeoName.ceo.name;
catsIncWithCeoName.removeCeoNameType().ceo.name;
}
}

也许你可以这样做:

type Ceo = { name: string };

class Company {
    addCeoNameType() {
        return this as this & {
            ceo: Ceo
        }
    }
    removeCeoNameType() {
        return this as unknown as Omit<this, 'ceo'> & { ceo: Omit<Ceo, 'name'> } ;
    }
}

class Foo {
    bar() {
        const catsInc = new Company();
        const catsIncWithCeoName = catsInc.addCeoNameType();
        catsIncWithCeoName.ceo.name;
        catsIncWithCeoName.removeCeoNameType().ceo.name;
                                                // ^ fails here
    }
}
type Ceo={name:string};
阶级公司{
addCeoNameType(){
以this&{
首席执行官:首席执行官
}
}
removeCeoNameType(){
以未知形式返回此值作为Omit&{ceo:Omit};
}
}
福班{
bar(){
const catsInc=新公司();
const catsIncWithCeoName=catsInc.addCeoNameType();
catsIncWithCeoName.ceo.name;
catsIncWithCeoName.removeCeoNameType().ceo.name;
//^此处失败
}
}

也许你可以这样做:

type Ceo = { name: string };

class Company {
    addCeoNameType() {
        return this as this & {
            ceo: Ceo
        }
    }
    removeCeoNameType() {
        return this as unknown as Omit<this, 'ceo'> & { ceo: Omit<Ceo, 'name'> } ;
    }
}

class Foo {
    bar() {
        const catsInc = new Company();
        const catsIncWithCeoName = catsInc.addCeoNameType();
        catsIncWithCeoName.ceo.name;
        catsIncWithCeoName.removeCeoNameType().ceo.name;
                                                // ^ fails here
    }
}
type Ceo={name:string};
阶级公司{
addCeoNameType(){
以this&{
首席执行官:首席执行官
}
}
removeCeoNameType(){
以未知形式返回此值作为Omit&{ceo:Omit};
}
}
福班{
bar(){
const catsInc=新公司();
const catsIncWithCeoName=catsInc.addCeoNameType();
catsIncWithCeoName.ceo.name;
catsIncWithCeoName.removeCeoNameType().ceo.name;
//^此处失败
}
}