Here's a similar question with an answer: React with TypeScript - define defaultProps in stateless function

import React, { Component } from 'react';
import { Text } from 'react-native';

interface TestProps {
    title?: string,
    name?: string
}

const defaultProps: TestProps = {
    title: 'Mr',
    name: 'McGee'
}

const Test: React.SFC<TestProps> = (props) => (
    <Text>
        {props.title} {props.name}
    </Text>
);

Test.defaultProps = defaultProps;

export default Test;
Answer from Matt Stow on Stack Overflow
🌐
Meje
meje.dev › blog › optional-props-in-typescript
Optional props in TypeScript
April 24, 2023 - import React from 'react' import { ReactSVG } from 'react-svg' interface iconProp { name: string dashboard: boolean } export const Icon: React.FC<iconProp> = ({ name, dashboard }) => { return ( <ReactSVG src={ dashboard ? `/icons/dashboard-icons/${name}.svg` : `/icons/${name}.svg` } /> ) } At first, I thought adding boolean as a value to dashboard would solve this issue of it being an optional prop, until I started getting build errors because TypeScript was screaming that the prop, dashboard, is required.
Discussions

React-Typescript complaining about optional props missing (defined with prop-types)
You need ?: to flag a property as optional. Otherwise you MUST add it, even if you give it the value undefined. There is a difference in checking for the key. interface Foo { requiredProperty: any; optionalProperty?: string; } const someObject: Foo = getFoo(); if ("requiredProperty" in someObject) // always true { // the value may be undefined console.log(someObject.requiredProperty); } if ("optionalProperty" in someObject) { // the value cannot be undefined console.log(someObject.optionalProperty); } // the value may be undefined console.log(someObject.optionalProperty); More on reddit.com
🌐 r/typescript
11
5
May 5, 2022
reactjs - How to pass this TypeScript React component an optional prop? - Stack Overflow
React.memo is more for values which you don't want to have to recompute. ... Sign up to request clarification or add additional context in comments. ... The Object where you are trying to add optional prop is a destructured props object and not the type of props object. More on stackoverflow.com
🌐 stackoverflow.com
Adding optional props to a React component.

You can simplify it even further by replacing all those props for an optional group prop (and the original name prop), and keeping it to one ternary operator (generally try to avoid nesting them as it’s harder to read)

group ? `icons/${group}/${name}` : `icons/${name}`

I’m on mobile hope I formatted this correctly

More on reddit.com
🌐 r/reactjs
2
0
April 24, 2023
Optional types on react component showing undefined as value
It isn't possible, as far as I know, without some extreme complexity. You'd basically need to define two separate types, one with and one without appearance, and do some fancy footwork to tell the compiler which one is being requested. All in all, it's not worth the complexity of the resulting type to erase a common TS error situation which is actually quite valid. You absolutely can use appearance={undefined} and it will work without a hitch. If anything, I'd ask your peer how to do it, and watch them stutter. More on reddit.com
🌐 r/reactjs
4
1
October 11, 2023
🌐
DEV Community
dev.to › lico › i-ruined-my-react-components-by-using-optional-props-3o64
I ruined my React components by using optional props - DEV Community
December 22, 2022 - type: There are two options primary and secondary. We can expect from the name that primary would be used if we don't pass the prop. import { ReactNode } from 'react'; type ColorButtonColor = 'red' | 'blue'; interface ColorButtonProps { children?: ReactNode | string; onClick?: VoidFunction; color?: ColorButtonColor; } const ColorButton = ({ children, onClick, color = 'red', }: ColorButtonProps) => { return ( <button style={{ backgroundColor: color }} onClick={onClick}> {children} </button> ); }; export default ColorButton;
🌐
Reddit
reddit.com › r/typescript › react-typescript complaining about optional props missing (defined with prop-types)
r/typescript on Reddit: React-Typescript complaining about optional props missing (defined with prop-types)
May 5, 2022 -

Hi, I'm converting a React JS codebase to TS.

I just converted a component (`TabButton) that uses an Icon component that is not converted yet but uses prop-types. The problem is that TS (in TabButton) is complaining that Icon misses some props but they should be completely optional as in Icon I didn't require them.

The error text is: Type '{ component: ReactNode; }' is missing the following properties from type '{ [x: string]: any; component: any; color: any; hoverColor: any; className: any; onClick: any; }': color, hoverColor, className, onClick ts(2739)

What can I do? Thanks

TabButton.tsx Hovering over Icon in TabButton.tsx Icon.js
🌐
DhiWise
dhiwise.com › post › how-react-optional-props-can-improve-the-maintainability
Embracing Optional Props for Robust React Applications
June 14, 2024 - Using an optional prop interface in TypeScript enhances type safety by explicitly defining which props are optional. This helps prevent runtime errors and assists developers in understanding the component's API.
🌐
Medium
chrisfrewin.medium.com › react-with-typescript-optional-props-with-default-values-cf4c8370659f
React with TypeScript: Optional Props with Default Values | by Chris Frewin | Medium
October 10, 2021 - The component has a required title string, and then a few optional style options. We can leverage the pattern discussed in the post to build the following: import * as React from "react";// Required props interface IFancyTitleRequiredProps { title: string; }// Optional props interface IFancyTitleOptionalProps { color: string; fontSize: number; }// Combine required and optional props to build the full prop interface interface IFancyTitleProps extends IFancyTitleRequiredProps, IFancyTitleOptionalProps {}// Use the optional prop interface to define the default props const defaultProps: IFancyTitl
🌐
Steve Kinney
stevekinney.com › courses › react with typescript › complete guide to react component props with typescript
Complete Guide to React Component Props with TypeScript | React with TypeScript | Steve Kinney
March 17, 2026 - The most fundamental decision for any prop is whether it’s required or optional. TypeScript makes this distinction explicit, and it’s worth thinking carefully about each prop’s necessity. interface ButtonProps { // Required props - must be provided children: React.ReactNode; onClick: () => void; // Optional props - can be omitted variant?: 'primary' | 'secondary' | 'danger'; disabled?: boolean; size?: 'small' | 'medium' | 'large'; } // ✅ Good - required props are provided <Button onClick={() => alert('clicked')}> Click me </Button> // ❌ Bad - TypeScript error: missing required 'onClick' <Button> Click me </Button>
🌐
DEV Community
dev.to › fullstackchris › react-with-typescript-optional-props-with-default-values-33nc
React with TypeScript: Optional Props with Default Values - DEV Community
October 10, 2021 - We can then define our default props values by only taking the IMyComponentOptionalProps: const defaultProps: IMyComponentOptionalProps = { color: "red", fontSize: 40, }; and then being sure to set these defaultProps to the component's defaultProps: ... Let's see this pattern in an actual React component. Here's the example component FancyTitle from the example repository which renders a customizable <h1> tag. The component has a required title string, and then a few optional style options.
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › react-optional-props-typescript
Set optional props with default values in React TypeScript | bobbyhadz
To set optional props with default values in React TypeScript, mark the props on the type as optional using a question mark.
🌐
O'Reilly
oreilly.com › library › view › learn-react-with › 9781789610253 › 37d6aed2-5d7b-4d0e-a4d9-1c8892c83c0b.xhtml
Optional props - Learn React with TypeScript 3 [Book]
November 29, 2018 - We put a ? before the type annotation to denote that the prop is optional. Note also that we don't get a compilation error in App.tsx, where we reference, Confirm because we are not required to enter these as attributes on Confirm. Let's reference these props in our JSX now, ...
Author   Carl Rippon
Published   2018
Pages   502
🌐
DEV Community
dev.to › bytebodger › default-props-in-react-typescript-2o5o
Default Props in React/TypeScript - DEV Community
July 29, 2020 - A dead-simple component that accepts up to 5 props, with 2 of those props being required. For the 3 optional props, default values are assigned. If the component is wrapping other content, that content will be rendered with props.children. This is basically React 101. So let's set about converting this to TypeScript.
🌐
Levelup
levelup.video › tutorials › react-and-typescript-for-everyone › default-and-optional-props
React & TypeScript For Everyone
Now that we have our prop types setup, we will take a look at default and optional props. By using the default and optional props, we can further ensure that our React components will receive the exact type of props that we expect.
🌐
React TypeScript Cheatsheets
react-typescript-cheatsheet.netlify.app › typing defaultprops
Typing defaultProps | React TypeScript Cheatsheets
// you can also declare the type of DefaultProps if you choose // e.g. https://github.com/typescript-cheatsheets/react/issues/415#issuecomment-841223219 type GreetProps = { age: number } & typeof defaultProps; const defaultProps = { age: 21, }; const Greet = (props: GreetProps) => { // etc }; Greet.defaultProps = defaultProps;
🌐
LogRocket
blog.logrocket.com › home › a complete guide to react default props
A complete guide to React default props - LogRocket Blog
June 4, 2024 - This is because the select menu needs defaultValue, but because there is no optional prop to provide it (I removed the defaultValue variable, if you didn’t notice), it uses defaultDefaultValue, which is a default prop instead. ... Default props are a great way to make your React components reusable and easier to maintain.
Top answer
1 of 3
3

What you're thinking of is declaring an optional property of a type. For instance:

interface Props { bgColor?: string }
const a: Props = { bgColor: '#fff' } // valid
const b: Props = {}                  // valid

But the only type in your code here is any, which is not a good practice to use as it disables all type checking.

So what you want to do is delcare the type for your props, which includes the optional property:

interface Props {
    src: string,
    children: React.ReactNode
    bgColor?: string
}

Then use that type.

export function BGVideo({ src, children, bgColor }: Props) {
  return (
    <>...</>
  )
}

Now in that function, bgColor has the type string | undefined. A string if it was passed a value, or undefined if it was not passed a value.

Working example


Lastly, React.memo really isn't necesary. You shouldn't really ever need to wrap a function component in this way. React.memo is more for values which you don't want to have to recompute.

2 of 3
1

The Object where you are trying to add optional prop is a destructured props object and not the type of props object.

You can add type for props object as follows:

export const BGVideo = React.memo(function BGVideo({ src, children, bgColor }: {src: string, children: React.ReactNode, bgColor?: string}) {
  return (
    <BackgroundVideoContainer>...some other stuff...
  )
});

Or better extract the type in a interface or type for better readability:

interface BGVideoProps {
      src: string; 
      children: React.ReactNode; 
      bgColor?: string;
    }

export const BGVideo = React.memo(function BGVideo({ src, children, bgColor }: BGVideoProps) {   
  return (
        <BackgroundVideoContainer>...some other stuff...   ) 
});

Here, bgColor is a optional prop

🌐
Better Programming
betterprogramming.pub › 5-recipes-for-setting-default-props-in-react-typescript-b52d8b6a842c
5 Recipes for Setting Default Props in React and TypeScript | by Guillaume Renard | Better Programming
September 28, 2022 - Similar to the previous example, ... Here is a pattern that I like to use in my unit tests. It uses the Partial type of TypeScript to make all properties optional (even the ones that are not marked optional)....
🌐
Webdevtutor
webdevtutor.net › blog › typescript-react-component-optional-props
Utilizing Optional Props in TypeScript React Components
This can be particularly useful when dealing with components that have varying appearances or behaviors based on different sets of props. In TypeScript, you can denote a prop as optional by adding a question mark (?) after its name in the component's prop interface.