【问题标题】:How to type nested objects - Typescript如何键入嵌套对象 - Typescript
【发布时间】:2022-11-30 03:14:23
【问题描述】:

我在输入这行代码 initial State[a][b] 时遇到问题。

我收到此错误:

元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{ food: { pizza: boolean;鸡:布尔值; };运输:{总线:布尔值;汽车:布尔值; }; }'

function testTypescript(a: string, b: string) {
    const initialState = {
        food: {
            pizza: false,
            chicken: false,
        },
        transport: {
            bus: false,
            car: false,
        },
    };
    const newData = !initialState[a][b]; // How can I type this line?
    const newState = { ...initialState, [a]: newData };
    return newState;
}

【问题讨论】:

    标签: typescript


    【解决方案1】:

    您可以使用一些泛型。 Play

    type State = {
      food: {
        pizza: boolean;
        chicken: boolean;
      };
    
      transport: {
        bus: boolean;
        car: boolean;
      }
    }
    
    function testTypescript<T extends keyof State>(a: T, b: keyof State[T]) {
        const initialState: State = {
            food: {
                pizza: false,
                chicken: false,
            },
            transport: {
                bus: false,
                car: false,
            },
        };
        const newData = !initialState[a][b]; // How can I type this line?
        const newState = { ...initialState, [a]: newData };
        return newState;
    }
    
    // @ts-expect-error
    testTypescript('notThere', 'value')
    
    // @ts-expect-error
    testTypescript('food', 'rice')
    
    testTypescript('transport', 'bus')
    

    【讨论】:

      【解决方案2】:
      function testTypescript(a: string, b: string) {
          const initialState: { [a: string]: { [b: string]: boolean; } } = {
              food: {
                  pizza: false,
                  chicken: false,
              },
              transport: {
                  bus: false,
                  car: false,
              },
          };
          const newData: boolean = !initialState[a][b];
          const newState = { ...initialState, [a]: { [b]: newData } };
          return newState;
      }
      

      【讨论】:

        【解决方案3】:

        任意嵌套

        type initial = {
          [key: string] :  any;
        }
        
        function testTypescript(a: string, b: string) {
            const initialState : initial = {
                food: {
                    pizza: false,
                    chicken: false,
                },
                transport: {
                    bus: false,
                    car: false,
                },
            };
            const newData = !initialState[a][b]; // How can I type this line?
            const newState = { ...initialState, [a]: newData };
            return newState;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-02
          • 2018-11-05
          • 2021-10-21
          • 1970-01-01
          • 1970-01-01
          • 2021-08-27
          相关资源
          最近更新 更多