账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    求教一个typescript的类型定义问题
    64
    0
    我定义了三个类型如下:type ParamsA = { same: string; a: string;};type ParamsB = { same: string; b: number;};type Common = { c: number;};两个方法:function fnA(data: ParamsA & Common) { console.log(data);}function fnB(data: ParamsB & Common) { console.log(data);}然后我在页面使用的时候:type Json = Partial<ParamsA & ParamsB>;const randomBoolean = true; // 这里可能是true,也可能是falseconst json: Json = {};function fnc() { // 这里可以保证json里面的值根据randomBoolean的值对应的类型都有 // 例如,为true的时候就是肯定有same a c,但是b不一定会有 const params = { ...json, c: 1, }; if (randomBoolean) { delete params.b; } else { delete params.a; } const f = randomBoolean ? fnA : fnB; f(params);}问题就是我在使用f(params)的时候怎么让这个params有上面方法里面定义的类型?我尝试写了一个方法去推导出类型,但是还是不行,请大佬解答一二。declare function filterParams<T>( x: Json): T extends true ? ParamsA & Common : ParamsB & Common;// 使用的时候f(filterParams<typeof randomUnm>(params))
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 薄荷成瘾者 普通会员 1楼

      在 TypeScript 中,类型定义是一种将变量、函数、类、接口等声明为具体类型的方法。以下是一个简单的类型定义示例:

      ```typescript // 声明一个类型为 number 的变量 let myNumber: number = 42;

      // 声明一个函数,参数为 number 类型 function add(a: number, b: number): number { return a + b; }

      // 声明一个类,其中包含一个属性和一个方法 class MyClass { private myProperty: number; private myMethod: number;

      constructor(private value: number) { this.myProperty = value; this.myMethod = add; }

      public getMyProperty(): number { return this.myProperty; }

      public setMyProperty(value: number): void { this.myProperty = value; }

      public addMyMethod(a: number, b: number): number { return this.myMethod(a, b); } }

      // 测试类型定义 console.log(myNumber); // 输出:42 console.log(add(3, 4)); // 输出:7 console.log(MyClass.prototype.myProperty); // 输出:42 console.log(MyClass.prototype.myMethod); // 输出:7 ```

      在这个示例中,我们首先声明了一个名为 myNumber 的变量,并将其类型声明为 number。接着,我们定义了一个名为 add 的函数,该函数接受两个参数 ab,并返回它们的和。我们还声明了一个名为 MyClass 的类,该类包含一个名为 myProperty 的私有属性和一个名为 myMethod 的私有方法。

      MyClass 类的构造函数中,我们为 myPropertymyMethod 声明了默认值。在 getMyProperty 方法中,我们返回 myProperty 的值。在 setMyProperty 方法中,我们修改 myProperty 的值。在 addMyMethod 方法中,我们调用了 myMethod 方法,并将两个参数传递给它。

      最后,我们通过 console.log 打印了每个变量和方法的类型。在这个示例中,myNumber 的类型为 numberadd 的类型为 functionMyClass 的类型为 classmyProperty 的类型为 numbermyMethod 的类型为 function

    • 李豫 普通会员 2楼

      在 TypeScript 中,类型定义是一种将变量、函数、类、接口等声明为具体类型的方法。以下是一个简单的类型定义示例:

      ```typescript // 声明一个类型为 number 的变量 let myNumber: number = 42;

      // 声明一个函数,参数为 number 类型 function add(a: number, b: number): number { return a + b; }

      // 声明一个类,其中包含一个属性和一个方法 class MyClass { private myProperty: number; private myMethod: number;

      constructor(private value: number) { this.myProperty = value; this.myMethod = add; }

      public getMyProperty(): number { return this.myProperty; }

      public setMyProperty(value: number): void { this.myProperty = value; }

      public addMyMethod(a: number, b: number): number { return this.myMethod(a, b); } }

      // 测试类型定义 console.log(myNumber); // 输出:42 console.log(add(3, 4)); // 输出:7 console.log(MyClass.prototype.myProperty); // 输出:42 console.log(MyClass.prototype.myMethod); // 输出:7 ```

      在这个示例中,我们首先声明了一个名为 myNumber 的变量,并将其类型声明为 number。接着,我们定义了一个名为 add 的函数,该函数接受两个参数 ab,并返回它们的和。我们还声明了一个名为 MyClass 的类,该类包含一个名为 myProperty 的私有属性和一个名为 myMethod 的私有方法。

      MyClass 类的构造函数中,我们为 myPropertymyMethod 声明了默认值。在 getMyProperty 方法中,我们返回 myProperty 的值。在 setMyProperty 方法中,我们修改 myProperty 的值。在 addMyMethod 方法中,我们调用了 myMethod 方法,并将两个参数传递给它。

      最后,我们通过 console.log 打印了每个变量和方法的类型。在这个示例中,myNumber 的类型为 numberadd 的类型为 functionMyClass 的类型为 classmyProperty 的类型为 numbermyMethod 的类型为 function

    • 寂寞无罪 普通会员 3楼

      在 TypeScript 中,类型定义是一种将变量、函数、类、接口等声明为具体类型的方法。以下是一个简单的类型定义示例:

      ```typescript // 声明一个类型为 number 的变量 let myNumber: number = 42;

      // 声明一个函数,参数为 number 类型 function add(a: number, b: number): number { return a + b; }

      // 声明一个类,其中包含一个属性和一个方法 class MyClass { private myProperty: number; private myMethod: number;

      constructor(private value: number) { this.myProperty = value; this.myMethod = add; }

      public getMyProperty(): number { return this.myProperty; }

      public setMyProperty(value: number): void { this.myProperty = value; }

      public addMyMethod(a: number, b: number): number { return this.myMethod(a, b); } }

      // 测试类型定义 console.log(myNumber); // 输出:42 console.log(add(3, 4)); // 输出:7 console.log(MyClass.prototype.myProperty); // 输出:42 console.log(MyClass.prototype.myMethod); // 输出:7 ```

      在这个示例中,我们首先声明了一个名为 myNumber 的变量,并将其类型声明为 number。接着,我们定义了一个名为 add 的函数,该函数接受两个参数 ab,并返回它们的和。我们还声明了一个名为 MyClass 的类,该类包含一个名为 myProperty 的私有属性和一个名为 myMethod 的私有方法。

      MyClass 类的构造函数中,我们为 myPropertymyMethod 声明了默认值。在 getMyProperty 方法中,我们返回 myProperty 的值。在 setMyProperty 方法中,我们修改 myProperty 的值。在 addMyMethod 方法中,我们调用了 myMethod 方法,并将两个参数传递给它。

      最后,我们通过 console.log 打印了每个变量和方法的类型。在这个示例中,myNumber 的类型为 numberadd 的类型为 functionMyClass 的类型为 classmyProperty 的类型为 numbermyMethod 的类型为 function

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部