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
🌐
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.
Discussions

Default props not working in functional component
TypeScript Version: 3.4.5 Search Terms: react default props defaultProps functional component stateless Code import React from "react"; interface Props { name: string; optional: string; } const Com... More on github.com
🌐 github.com
20
May 4, 2019
reactjs - Default property value in React component using TypeScript - Stack Overflow
This works the same but you don't ... required props (even though those defaults will never be used) or omit the explicit type annotation completely. You can use defaultProps on function components as well, but you have to type your function to the FunctionComponent (StatelessComponent in @types/react before version 16.7.2) interface so that TypeScript knows about ... More on stackoverflow.com
🌐 stackoverflow.com
How can I in React Native Typescript pass optional props(that are valid choices) and spread to a inner TextInput?
You can use TextInputProps from react-native and intersection types so: import { TextInputProps } from 'react-native'; ... type Props = { style: ViewStyle; // btw you can use style: StyleProp; } & TextInputProps; More on reddit.com
🌐 r/reactnative
5
2
April 9, 2022
Error TS2532 and Optional React Component Props in TypeScript
IMO, this is far from safe or best practice. Using ! to tell the compiler, "nah, we're good" comes back to bite you more often than not. Using as to make your DefaultProps behave like your ComponentProps is very brittle. Instead of that, define an interface for the props that will be provided when your component is instantiated, define another for the default props, provide the combination of the two to the Component generic, and then export the class with only your ComponentProps. Your component's public face will behave exact as you want and the DefaultProps are a safe, private detail. This is the same process you'd take if you wanted to ignore the common Redux container/component advice and instantiate a Redux-aware component elsewhere without burdening the caller with its props. interface ComponentProps { age?: number; name: string; } interface DefaultProps { age: number } interface LocalState { # your state } class MyComponent extends Component { # define the expected shape of this static member, DON'T use `as` to override the compiler # let the compiler do its job and keep an eye on you static defaultProps: DefaultProps = { age: 33 } # demonstrating that we need to match our generic above or we will get an error # this is true of all React lifecycle methods that take props as an argument constructor(initialProps: ComponetProps & DefaultProps) { super(initialProps); this.state = { # your state here } } } # Down here, we DO want to tell the compiler how to present this component to the world, so `as` is the way to go export default MyComponent as React.ComponentClass; More on reddit.com
🌐 r/typescript
5
7
November 30, 2017
🌐
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 - There were too many options and I wasn't sure what I was going to use to make the component work properly. I also used unexpected default values a lot. It makes plenty of unnecessary defensive code and I couldn't expect what default values it has. I will show you some examples that are similar to the mistakes that I made. Let's say, there is a Button component like below. import { ReactNode } from 'react'; type ButtonType = 'primary' | 'secondary'; interface ButtonProps { children?: ReactNode | string; onClick?: VoidFunction; type?: ButtonType; } const Button = ({ children, onClick, type = 'primary' }: ButtonProps) => { const bgColor = type === 'primary' ?
🌐
React TypeScript Cheatsheets
react-typescript-cheatsheet.netlify.app › typing defaultprops
Typing defaultProps | React TypeScript Cheatsheets
For TypeScript 2.9 and earlier, there's more than one way to do it, but this is the best advice we've yet seen: type Props = Required<typeof MyComponent.defaultProps> & { /* additional props here */ }; export class MyComponent extends React.Component<Props> { static defaultProps = { foo: "foo", }; }
🌐
Meje
meje.dev › blog › optional-props-in-typescript
Optional props in TypeScript
April 24, 2023 - Take a look at the updated component below: 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.
🌐
Bobby Hadz
bobbyhadz.com › blog › react-optional-props-typescript
Set optional props with default values in React TypeScript | bobbyhadz
Copied!interface EmployeeProps ... the name and age props as optional. This means that the component can be used with or without providing the name and age props....
🌐
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 - 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> The rule of thumb: make props required when the component genuinely can’t function without them.
🌐
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.
Find elsewhere
🌐
Pluralsight
pluralsight.com › what makes pluralsight different | pluralsight › tech guides & tutorials
Defining Props in React Function Component with Typescript | Pluralsight
August 25, 2020 - Learn how to define props for function components in React projects with Typescript. We cover several different methods, including destructuring and React.FC. ... This guide will help you properly define the props entering your function components. You'll also learn when defining props as a class or interface type is best and how to provide default values to optional props.
🌐
Delft Stack
delftstack.com › home › howto › typescript › typescript optional props in react
How to Use Optional Props With Default Values in React | Delft Stack
February 2, 2024 - In TypeScript, a type or prop can be made optional using the ? operator. This fact can be used to pass optional parameters in React. interface OptionalComponentProps { color? : string; title : string; size?
🌐
CopyProgramming
copyprogramming.com › howto › how-to-specify-optional-default-props-with-typescript-for-stateless-functional-react-components
TypeScript Optional Props in React Components: Complete Guide for 2026
November 5, 2025 - The React team has deprecated defaultProps in functional components in favor of JavaScript default parameters. While still functional, it produces console warnings in React 18+ and will be removed in a future version. Default parameters are cleaner, more TypeScript-friendly, and work seamlessly with modern React patterns. Q: How do I handle optional props that shouldn't have defaults?
🌐
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 - For example, if our component is called MyComponent: // Required props interface IMyComponentRequiredProps {}// Optional props interface IMyComponentOptionalProps {} We can then use the TypeScript extends operator to combine these two interfaces to the final props interface we would use to actually type the props parameter:
🌐
DEV Community
dev.to › bytebodger › default-props-in-react-typescript-2o5o
Default Props in React/TypeScript - DEV Community
July 29, 2020 - While the above code indeed "works" just fine, I found that things started to get wonky when I began adding functions inside the functional component. Consider this example: interface Props extends PropsWithChildren<any>{ requiredString: string, requiredNumber: number, optionalBoolean?: boolean, optionalString?: string, optionalNumber?: number, } export default function MyTSComponent(props: Props) { props = cloneObject(props); props.optionalBoolean = props.optionalBoolean !== undefined ?
🌐
GitHub
github.com › microsoft › TypeScript › issues › 31247
Default props not working in functional component · Issue #31247 · microsoft/TypeScript
May 4, 2019 - TypeScript Version: 3.4.5 · Search ... 3.0 release notes, the optional prop should not be required in Test as it has been defined with a default using the ES2015 default initializers feature in Component....
Author   AzureMarker
🌐
tutorialpedia
tutorialpedia.org › blog › how-to-specify-optional-default-props-with-typescript-for-stateless-functional-react-components
How to Specify Optional Default Props with TypeScript for Stateless Functional React Components — tutorialpedia.org
Specifying optional default props with TypeScript for stateless functional components ensures your UI is robust, self-documenting, and free of undefined errors. By combining TypeScript’s type safety with React’s default parameters, you create components that are both flexible and reliable.
🌐
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.
🌐
LogRocket
blog.logrocket.com › home › a complete guide to react default props
A complete guide to React default props - LogRocket Blog
June 4, 2024 - Optional props are not mandatory and are useful when certain props are not essential for the component to function. Our HeaderParent component, as defined here, passes an optional prop, version to its child component ReactHeader:
🌐
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 - Note that you don’t have to specify a type for fromIndex since TypeScript implicitly assumes it’s a number thanks to the default value (0). The same technique also works for functional components (they are functions after all). So, for the sake of completeness, let’s look at a quick example too. The component in question has an optional variant property, which is used to determine what style to apply to it.
Top answer
1 of 12
431

Default props with class component

Using static defaultProps is correct. You should also be using interfaces, not classes, for the props and state.

Update 2018/12/1: TypeScript has improved the type-checking related to defaultProps over time. Read on for latest and greatest usage down to older usages and issues.

For TypeScript 3.0 and up

TypeScript specifically added support for defaultProps to make type-checking work how you'd expect. Example:

interface PageProps {
  foo: string;
  bar: string;
}

export class PageComponent extends React.Component<PageProps, {}> {
    public static defaultProps = {
        foo: "default"
    };

    public render(): JSX.Element {
        return (
            <span>Hello, { this.props.foo.toUpperCase() }</span>
        );
    }
}

Which can be rendered and compile without passing a foo attribute:

<PageComponent bar={ "hello" } />

Note that:

  • foo is not marked optional (ie foo?: string) even though it's not required as a JSX attribute. Marking as optional would mean that it could be undefined, but in fact it never will be undefined because defaultProps provides a default value. Think of it similar to how you can mark a function parameter optional, or with a default value, but not both, yet both mean the call doesn't need to specify a value. TypeScript 3.0+ treats defaultProps in a similar way, which is really cool for React users!
  • The defaultProps has no explicit type annotation. Its type is inferred and used by the compiler to determine which JSX attributes are required. You could use defaultProps: Pick<PageProps, "foo"> to ensure defaultProps matches a sub-set of PageProps. More on this caveat is explained here.
  • This requires @types/react version 16.4.11 to work properly.

For TypeScript 2.1 until 3.0

Before TypeScript 3.0 implemented compiler support for defaultProps you could still make use of it, and it worked 100% with React at runtime, but since TypeScript only considered props when checking for JSX attributes you'd have to mark props that have defaults as optional with ?. Example:

interface PageProps {
    foo?: string;
    bar: number;
}

export class PageComponent extends React.Component<PageProps, {}> {
    public static defaultProps: Partial<PageProps> = {
        foo: "default"
    };

    public render(): JSX.Element {
        return (
            <span>Hello, world</span>
        );
    }
}

Note that:

  • It's a good idea to annotate defaultProps with Partial<> so that it type-checks against your props, but you don't have to supply every required property with a default value, which makes no sense since required properties should never need a default.
  • When using strictNullChecks the value of this.props.foo will be possibly undefined and require a non-null assertion (ie this.props.foo!) or type-guard (ie if (this.props.foo) ...) to remove undefined. This is annoying since the default prop value means it actually will never be undefined, but TS didn't understand this flow. That's one of the main reasons TS 3.0 added explicit support for defaultProps.

Before TypeScript 2.1

This works the same but you don't have Partial types, so just omit Partial<> and either supply default values for all required props (even though those defaults will never be used) or omit the explicit type annotation completely.

Default props with Functional Components

You can use defaultProps on function components as well, but you have to type your function to the FunctionComponent (StatelessComponent in @types/react before version 16.7.2) interface so that TypeScript knows about defaultProps on the function:

interface PageProps {
  foo?: string;
  bar: number;
}

const PageComponent: FunctionComponent<PageProps> = (props) => {
  return (
    <span>Hello, {props.foo}, {props.bar}</span>
  );
};

PageComponent.defaultProps = {
  foo: "default"
};

Note that you don't have to use Partial<PageProps> anywhere because FunctionComponent.defaultProps is already specified as a partial in TS 2.1+.

Another nice alternative (this is what I use) is to destructure your props parameters and assign default values directly:

const PageComponent: FunctionComponent<PageProps> = ({foo = "default", bar}) => {
  return (
    <span>Hello, {foo}, {bar}</span>
  );
};

Then you don't need the defaultProps at all! Be aware that if you do provide defaultProps on a function component it will take precedence over default parameter values, because React will always explicitly pass the defaultProps values (so the parameters are never undefined, thus the default parameter is never used.) So you'd use one or the other, not both.

2 of 12
23

With Typescript 2.1+, use Partial < T > instead of making your interface properties optional.

export interface Props {
    obj: Model,
    a: boolean
    b: boolean
}

public static defaultProps: Partial<Props> = {
    a: true
};
🌐
xjavascript
xjavascript.com › blog › extend-react-functional-component-props-typescript
Extending React Functional Component Props with TypeScript — xjavascript.com
If a prop is not always required, make it optional using the ? operator. This makes your component more flexible. import React from 'react'; type MyProps = { requiredProp: string; optionalProp?: number; }; const MyComponent: React.FC<MyProps> ...