Ensure Props are Present and Defined
Props are one of the most important things you need to understand, because you'll work with them every day when you're using React with TypeScript.
Here we have a Button
component and these props are currently unknown
:
export const Button = (props: unknown) => { return <button
Transcript
0:00 In this exercise, we're going to learn about props because props is one of the most important things you'll need to be doing every day when you're using React with TypeScript.
0:09 Here we have a Button component, and these props are currently unknown. When we try to access className on props.className, we're getting an error here. The thing that we want here is we want our Button to always expect a className, so this should be an error.
0:26 I'm using ts-expect-error here to basically say that the next line below it should be erroring, but it's currently erroring because it isn't erroring. Does that make sense? We're expecting an error here, but we're not getting one.
0:39 This Button className, it's yelling at us because className does not exist on type IntrinsicAttributes, so how do we change this component up here to make sure that className is one of the props and to make sure it's always defined? Good luck.