Core API Overview > @adpt/core > SFCDeclProps
SFCDeclProps type
Helper type for declaring the props argument of a function component. (The type that users of your component will see.)
Signature:
export declare type SFCDeclProps<Props, Defaults extends object = object> = Defaultize<Props, Defaults> & Partial<BuiltinProps>;
Remarks
This helper type can be used to create the type for your function component's props
argument. It correctly handles the standard set of BuiltinProps and your component's defaultProps
so that users of your component can pass in props like key
and handle
and also not be required to pass in any props that are required, but have valid default values in defaultProps
.
This type should **only** be used to describe the first argument to your function component.
It should typically be used along with SFCBuildProps.
Type parameters:
Props
- The object type that describes the props your function component takes, not including any BuiltinProps. For props that your component requires, but has valid defaults set in defaultProps
, those properties should be required (not optional) in Props
.
Defaults
- The object type of your component's defaultProps
.
Example
interface MyProps {
required: string; // User is required to always set this prop
hasDefault: string; // User can optionally set this prop or get default
optional?: string; // User can optionally set this prop, but no default
}
const defaultProps = {
hasDefault: "thedefault"
}
// Types for the properties of the props argument below are:
// props.required string [required]
// props.hasDefault string [optional]
// props.optional string [optional]
// props.key string [optional]
// props.handle Handle [optional]
function MyComponent(props: SFCDeclProps<MyProps, typeof defaultProps) {
// Types for the properties of the buildProps variable below are:
// buildProps.required string
// buildProps.hasDefault string
// buildProps.optional string | undefined
// buildProps.key string
// buildProps.handle Handle
const buildProps = props as SFCBuildProps<MyProps, typeof defaultProps>;
...
}
MyComponent.defaultProps = defaultProps;