Created
September 7, 2024 05:18
-
-
Save huynhducduy/009bf5608d52fc95a912ad1debc9bca4 to your computer and use it in GitHub Desktop.
Structural Typing vs Excess Property Check https://talohana.com/blog/typescript-excess-property-checks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Data = { | |
id: string; | |
}; | |
const data: Data = { | |
id: "123", | |
name: "Hello!", | |
} | |
const data2 = { | |
id: "123", | |
name: "Hello!", | |
}; | |
const data3: Data = data2; | |
type Func = () => { | |
id: string; | |
}; | |
const func1 = (): Data => { | |
return { | |
id: "123", | |
// No error on an excess property! | |
name: "Hello!", | |
}; | |
}; | |
const func2: Func = () => { | |
return { | |
id: "123", | |
// No error on an excess property! | |
name: "Hello!", | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment