How can I ensure that data2 and data3 work correctly, while data1 triggers an error if the data type is not specified as shown in the code below?
I need to validate that when a user enters params like {name: 'aa', age: 20}
, it should throw an error because the parameters must include a data type date (with any key name) like
{name: 'aa', age: 20, born: new Date('10-10-2020')
const data1 = {
name: 'Lorem',
age: 12
}
const data2 = {
name: 'Lorem',
age: 12,
born: new Date('10/10/1990')
}
const data3 = {
name: 'epsum',
age: 12,
died: new Date('10/10/1990')
}
interface PropsA {
name: string;
age: number;
}
const test = (inputParams: PropsA & { [x: string]: Date }) => {
return inputParams
}
test(data1) // It should result in an error since it lacks a value with data type Date apart from age and name
// How can I correct this error and make it work?
test(data2)
test(data3)