Skip to content

Instantly share code, notes, and snippets.

@boxpositron
Created June 8, 2023 13:32
Show Gist options
  • Save boxpositron/e9c7bd6b791a65b1c24180a95f089c87 to your computer and use it in GitHub Desktop.
Save boxpositron/e9c7bd6b791a65b1c24180a95f089c87 to your computer and use it in GitHub Desktop.
Partial Interfaces for Component in Typescript

Partial Interfaces Example

type NestedProperty = {
  foo: string;
  bar: string;
};

interface ComponentOptions {
  foobar: NestedProperty;
  foobaz: boolean;
}

const DEFAULT_OPTIONS: ComponentOptions = {
  foobar: {
    foo: "foo",
    bar: "bar",
  },
  foobaz: false,
};

const Component = (options: Partial<ComponentOptions>): ComponentOptions => {
  const mergedOptions = Object.assign({}, DEFAULT_OPTIONS, options);

  return mergedOptions;
};

Component({
  foobaz: true,
});
@harnie2275
Copy link

thanks i would try it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment