如何正确类型的检查的嵌套记录,允许分子树与稿?

0

的问题

请看看在下列演示。
interface Data 定义一架构对于嵌套数据。
function check 应验证是否有一个赋予分子树的这个 Data 结构很好,扔一个编译时间错误,如果不(希望与一个或多或少的详细的和可以理解的错误信息,而不仅仅是"...不是分配给类型的'不可失,时不再来").

interface Data {
  namespace1: {
    keyA: string,
    keyB: string
  },

  namespace2: {
    keyC: string,
    keyD: string
  }
}

// This function's only purpose is to perform a compile-time check
// whether the given partial data is valid.
// Returns the first and only argument in case of success,
// otherwise a compile-time error will occur.
function check<??>(
  partialData: ????
): ?????? {
  return partialData
}

// Example 1 => okay
const validPartialData1 = check({
  namespace1: {
    keyB: 'b'
  }
})

// Example 2 => okay
const validPartialData2 = check({
  namespace1: {
    keyB: 'b'
  },

  namespace2: {
    keyC: 'c'
  }
})

// Example 3 => okay
const validPartialData3 = check({})

// Example 4 => compile-time error!
const invalidPartialData1 = check({
  namespace1: {
    keyC: 'c'
  }
})

// Example 5 => compile-time error!
const invalidPartialData2 = check({
  xyz: {
    keyA: 'a'
  }
})
1

最好的答案

1

你不需要 check 功能。 使用可选择的领域。

interface Data {
  namespace1?: {
    keyA?: string,
    keyB?: string
  },

  namespace2?: {
    keyC?: string,
    keyD?: string
  }
}

const validPartialData1:Data = {
  namespace1: {
    keyB: 'b'
  }
}

看到 游乐场

如果你不想改变 Data 类型。 你可以定义的另一个 PartialData

type NestPartial<T> = {
    [P in keyof T]?: NestPartial<T[P]>;
}
type PartialData = NestPartial<Data>

const validPartialData1: PartialData = {
    namespace1: {
        keyB: 'b'
    }
}

看到 游乐场

2021-11-24 01:24:39

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................