首页 文章

TypeScript第二个参数类型基于第一个参数类型

提问于
浏览
2

我有第一个参数的可能值的枚举,我希望第二个参数基于第一个参数 . 所以,如果给出NAME,我希望第二个参数为字符串 . 如果给出了AGE,我希望第二个参数是一个数字 .

我怎么能这样做?

enum KeyType {
   NAME,
   AGE
}

class MyClass {
   public static setProperty(key: KeyType.NAME, value: string): void { }
   public static setProperty(key: KeyType.AGE, value: number): void { } 
}

我想要调用这样的方法:
MyClass.setProperty(KeyType.NAME, 'John');

此外,这应该显示一个错误:
MyClass.setProperty(KeyType.NAME, 5); // 5不是字符串

在此示例中,它不起作用,因为 key 类型被错误地定义(键类型实际上是枚举的值,因此键类型是 0 ) .

我也对有关使用仅允许特定参数键的特定类型的此功能的不同方法的建议持开放态度 .

4 回答

  • 0

    你可以试试这个

    MyClass.setProperty(key: KeyType, value: string |number) {
      // check key type and throw error as appropriate 
    }
    
  • -1

    javascript只包含对象,所以如果你想使用枚举,只需将它定义为和对象然后调用它:

    var enum1 = { SMALL:"asjdh", MEDIUM:2};
    console.log(enum1.SMALL)
    console.log(enum1.MEDIUM)
    
  • 3

    您希望使用函数重载来使类型检查正常工作:

    enum KeyType {
       NAME,
       AGE
    }
    
    class MyClass {
       public static setProperty(key: KeyType.NAME, value: string): void;
       public static setProperty(key: KeyType.AGE, value: number): void;
       public static setProperty(key: KeyType, value: (string | number)): void {}
    }
    

    或者更简单,只需使用字符串:

    class MyClass {
       public static setProperty(key: 'name', value: string): void;
       public static setProperty(key: 'age', value: number): void;
       public static setProperty(key: string, value: (string | number)): void {}
    }
    
    MyClass.setProperty('name', 42); // Argument of type '"name"' is not assignable to parameter of type '"age"'.
    MyClass.setProperty('age', 42); // Ok
    MyClass.setProperty('name', 'foo'); // Ok
    MyClass.setProperty('age', 'foo'); // Argument of type '"foo"' is not assignable to parameter of type 'number'.
    

    当然,您不必在重载中列出文字字符串,您可以将类似的字符串分组到类型定义中:

    type StringProperty = 'name' | 'address';
    type NumberProperty = 'age' | 'salary';
    class MyClass {
       public static setProperty(key: StringProperty, value: string): void;
       public static setProperty(key: NumberProperty, value: number): void;
       public static setProperty(key: string, value: (string | number)): void {}
    }
    
  • -1

    这种方法怎么样 - 如果你的类有两个属性,如age和name,你的setProperty函数可以获得字符串或年龄,并根据类型赋值给特定属性,例如:

    class MyClass {
        public age:number;
        public name:string;
    
        setProperty(value: string|number) {
            if (typeof value === 'string') {
                this.name = value;
            } else if (typeof value === 'number') {
                this.age = value;
            }
        }
    }
    
    let myClass = new MyClass();
    
    myClass.setProperty("John");
    console.log(myClass.name);
    myClass.setProperty(60);
    console.log(myClass.age);
    

    当然使用这个带有两种类型(字符串或数字)值的技巧你可以按照自己的意愿完成它:

    setProperty(key: string, value: string|number) {
        if (key === 'AGE') {
            this.age = value;
        } else if (key === 'NAME') {
            this.name = value;
        }
    }
    

    但在这种情况下,您需要编写更多代码来保护此部分 .

相关问题