“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”
Most TypeScript developers use TypeScript at a rudimentary level.
Don’t believe it? Try the daily challenge!
Today’s topic
// I have a bunch of required fields
interface DocType {
aaa: string.bbb: string.ccc: string
}
// Function type definition, waiting for you to rewrite...
function makeDoc(part1: any, part2: any) {
// ...
}
Copy the code
There is an interface called DocType that contains a set of required fields
We need to implement a function definition called makeDoc that takes part1 and part2, both of which have Partial
fields.
You need to implement type validation for two makeDoc parameters in TypeScript type definitions:
part1
和part2
After the fields are merged, i.eObject.assign({}, part1, part2)
To meet theDocType
Type definition of.part1
和part2
Cannot have duplicate fields in.
The test case
/ / legal
makeDoc({
aaa: '111'.bbb: '222'
}, {
ccc: '333'
})
/ / legal
makeDoc({
aaa: '111'.bbb: '222'.ccc: '333'
}, {})
/ / legal
makeDoc({ccc: '333'}, {
aaa: '111'.bbb: '222'
})
// An error should be reported (because required field CCC is missing)
makeDoc({
aaa: '111'}, {bbb: '222'
})
// An error should be reported (because the aaa field repeats)
makeDoc({
aaa: '111'.bbb: '222'
}, {
aaa: '333'.ccc: '444',})Copy the code
Feel free to leave your answers or thoughts in the comments section
Find out at 17:00 PM on Friday.