Si prega di dare un'occhiata al seguente demo.
interface Data
definisce uno schema di annidamento dei dati.
function check
deve verificare se un dato parziale sottostruttura di questo Data
la struttura è bella e gettare un errore di compilazione, se non, si spera, con una più o meno dettagliata e comprensibile il messaggio di errore e non solo "... non è assegnabile di tipo "mai").
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'
}
})