Skip to content

🎥 Make videos programmatically with React

props if has props

Jonny Burger8375bb4baf3e
diff --git a/packages/core/src/Composition.tsx b/packages/core/src/Composition.tsx
index 28a92ca1c8..965db50d02 100644
--- a/packages/core/src/Composition.tsx
+++ b/packages/core/src/Composition.tsx
@@ -14,6 +14,7 @@ import {Loading} from './loading-indicator.js';
import {NativeLayersContext} from './NativeLayers.js';
import {useNonce} from './nonce.js';
import {portalNode} from './portal-node.js';
+import type {PropsIfHasProps} from './props-if-has-props.js';
import {useLazyComponent} from './use-lazy-component.js';
import {useVideo} from './use-video.js';
import {validateCompositionId} from './validation/validate-composition-id.js';
@@ -35,8 +36,9 @@ export type StillProps<Schema extends z.ZodTypeAny, Props> = {
width: number;
height: number;
id: string;
- defaultProps?: z.infer<Schema> & Props;
-} & CompProps<Props>;
+ schema?: Schema;
+} & CompProps<Props> &
+ PropsIfHasProps<Schema, Props>;
type CompositionProps<Schema extends z.ZodTypeAny, Props> = StillProps<
Schema,
@@ -44,7 +46,6 @@ type CompositionProps<Schema extends z.ZodTypeAny, Props> = StillProps<
> & {
fps: number;
durationInFrames: number;
- schema?: Schema;
};
const Fallback: React.FC = () => {
@@ -60,7 +61,7 @@ const Fallback: React.FC = () => {
* @see [Documentation](https://www.remotion.dev/docs/composition)
*/
-export const Composition = <Schema extends z.ZodType, Props>({
+export const Composition = <Schema extends z.ZodTypeAny, Props>({
width,
height,
fps,
diff --git a/packages/core/src/CompositionManager.tsx b/packages/core/src/CompositionManager.tsx
index f1f18b44cf..41da223a2b 100644
--- a/packages/core/src/CompositionManager.tsx
+++ b/packages/core/src/CompositionManager.tsx
@@ -10,8 +10,9 @@ import React, {
} from 'react';
import type {z} from 'zod';
import type {TFolder} from './Folder.js';
+import type {PropsIfHasProps} from './props-if-has-props.js';
-export type TComposition<T extends z.ZodTypeAny, Props> = {
+export type TComposition<Schema extends z.ZodTypeAny, Props> = {
width: number;
height: number;
fps: number;
@@ -20,9 +21,8 @@ export type TComposition<T extends z.ZodTypeAny, Props> = {
folderName: string | null;
parentFolderName: string | null;
component: LazyExoticComponent<ComponentType<Props>>;
- defaultProps: (z.infer<T> & Props) | undefined;
nonce: number;
-};
+} & PropsIfHasProps<Schema, Props>;
export type AnyComposition = TComposition<z.ZodTypeAny, unknown>;
@@ -96,8 +96,8 @@ type BaseMetadata = Pick<
export type CompositionManagerContext = {
compositions: AnyComposition[];
- registerComposition: <T extends z.ZodTypeAny, Props>(
- comp: TComposition<T, Props>
+ registerComposition: <Schema extends z.ZodTypeAny, Props>(
+ comp: TComposition<Schema, Props>
) => void;
unregisterComposition: (name: string) => void;
registerFolder: (name: string, parent: string | null) => void;
diff --git a/packages/core/src/Still.tsx b/packages/core/src/Still.tsx
index dfcaf011fc..693a4fbc8f 100644
--- a/packages/core/src/Still.tsx
+++ b/packages/core/src/Still.tsx
@@ -7,7 +7,7 @@ import {Composition} from './Composition.js';
* @see [Documentation](https://www.remotion.dev/docs/still)
*/
-export const Still = <T extends z.ZodType, Props>(
+export const Still = <T extends z.ZodTypeAny, Props>(
props: StillProps<T, Props>
) => {
return <Composition fps={1} durationInFrames={1} {...props} />;
diff --git a/packages/core/src/props-if-has-props.ts b/packages/core/src/props-if-has-props.ts
new file mode 100644
index 0000000000..869d4c02b3
--- /dev/null
+++ b/packages/core/src/props-if-has-props.ts
@@ -0,0 +1,18 @@
+import type {z} from 'zod';
+
+export type PropsIfHasProps<
+ Schema extends z.ZodTypeAny,
+ Props
+> = z.ZodTypeAny extends Schema
+ ? {} extends Props
+ ? {
+ // Neither props nor schema specified
+ defaultProps?: z.infer<Schema> & Props;
+ }
+ : {
+ // Only props specified
+ defaultProps: Props;
+ }
+ : {
+ defaultProps: z.infer<Schema> & Props;
+ };
diff --git a/packages/example/src/comp-type-tests.tsx b/packages/example/src/comp-type-tests.tsx
new file mode 100644
index 0000000000..9a5494f181
--- /dev/null
+++ b/packages/example/src/comp-type-tests.tsx
@@ -0,0 +1,75 @@
+import React from 'react';
+import {Still, z} from 'remotion';
+// Test file just for making sure the types are sound
+
+type MyComponentProps = {
+ a: number;
+ b: string;
+};
+
+function MyComponent(props: MyComponentProps) {
+ return (
+ <div>
+ {props.a} {props.b}
+ </div>
+ );
+}
+
+<Still
+ component={MyComponent}
+ height={1080}
+ width={1080}
+ id="comps"
+ defaultProps={{a: 3, b: '2'}}
+/>;
+
+<Still
+ component={MyComponent}
+ height={1080}
+ width={1080}
+ id="comps"
+ // @ts-expect-error C should not be allowed
+ defaultProps={{c: '2'}}
+/>;
+
+// React.FC syntax
+const MyComponent2: React.FC<{
+ c: number;
+}> = ({c}) => {
+ return <div>{c}</div>;
+};
+
+<Still
+ component={MyComponent2}
+ height={1080}
+ width={1080}
+ id="comps"
+ defaultProps={{c: 2, d: '4'}}
+ schema={z.object({
+ d: z.string(),
+ })}
+/>;
+
+// React.FC syntax
+const MyComponent3: React.FC<{
+ a: number;
+}> = ({a}) => {
+ return <div>{a}</div>;
+};
+
+// No more optional types
+// @ts-expect-error Should be a type error
+<Still component={MyComponent3} height={1080} width={1080} id="hithere" />;
+<Still
+ component={MyComponent3}
+ height={1080}
+ width={1080}
+ id="hithere"
+ defaultProps={{a: 2}}
+/>;
+
+const CompWithNoProps: React.FC = () => {
+ return <div />;
+};
+
+<Still component={CompWithNoProps} height={1080} width={1080} id="no-props" />;
diff --git a/packages/player/src/utils/props-if-has-props.ts b/packages/player/src/utils/props-if-has-props.ts
index 1c2af34d89..8b004414e3 100644
--- a/packages/player/src/utils/props-if-has-props.ts
+++ b/packages/player/src/utils/props-if-has-props.ts
@@ -4,9 +4,15 @@ export type PropsIfHasProps<
Schema extends z.ZodTypeAny,
Props
> = z.ZodTypeAny extends Schema
- ? {
- inputProps?: z.infer<Schema> & Props;
- }
+ ? {} extends Props
+ ? {
+ // Neither props nor schema specified
+ inputProps?: z.infer<Schema> & Props;
+ }
+ : {
+ // Only props specified
+ inputProps: Props;
+ }
: {
inputProps: z.infer<Schema> & Props;
};