<?xml version="1.0" encoding="utf-8" ?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:tt="http://teletype.in/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"><title>Ars</title><author><name>Ars</name></author><id>https://teletype.in/atom/ars_kylnyk</id><link rel="self" type="application/atom+xml" href="https://teletype.in/atom/ars_kylnyk?offset=0"></link><link rel="alternate" type="text/html" href="https://teletype.in/@ars_kylnyk?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=ars_kylnyk"></link><link rel="next" type="application/rss+xml" href="https://teletype.in/atom/ars_kylnyk?offset=10"></link><link rel="search" type="application/opensearchdescription+xml" title="Teletype" href="https://teletype.in/opensearch.xml"></link><updated>2026-11-22T19:25:34.540Z</updated><entry><id>ars_kylnyk:NE1fd8etoeS</id><link rel="alternate" type="text/html" href="https://teletype.in/@ars_kylnyk/NE1fd8etoeS?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=ars_kylnyk"></link><title>Liskov Substitution Principle (LSP)</title><published>2025-05-05T12:23:13.355Z</published><updated>2025-05-05T12:23:13.355Z</updated><summary type="html">Liskov Substitution Principle (LSP)</summary><content type="html">
  &lt;p id=&quot;Giwh&quot;&gt;&lt;strong&gt;Liskov Substitution Principle (LSP)&lt;/strong&gt;&lt;/p&gt;
  &lt;ul id=&quot;NAkU&quot;&gt;
    &lt;li id=&quot;ugfh&quot;&gt;&lt;strong&gt;Definition&lt;/strong&gt;: Objects or components should be replaceable with instances of their subtypes without altering the correctness of the program.&lt;/li&gt;
    &lt;li id=&quot;eBE0&quot;&gt;&lt;strong&gt;In React&lt;/strong&gt;: When creating components or extending them, ensure they can be used interchangeably without breaking the application.&lt;/li&gt;
  &lt;/ul&gt;
  &lt;p id=&quot;RqHQ&quot;&gt;&lt;strong&gt;Not following LSP:&lt;/strong&gt; In this example, incorrect use of interface methods and not following the base interface for the same type of component violates the LSP principle.&lt;/p&gt;
  &lt;pre id=&quot;HgeN&quot;&gt;// ❌ BAD EXAMPLE - Violating LSP
interface BadBaseButton {
  onClick: () =&amp;gt; void;
  text: string;  // Restricts to string only
}

const BadBaseButton: React.FC&amp;lt;BadBaseButton&amp;gt; = ({ onClick, text }) =&amp;gt; (
  &amp;lt;button onClick={onClick}&amp;gt;{text}&amp;lt;/button&amp;gt;
);

// Violates LSP by changing prop structure and behavior
interface BadPrimaryButton {
  label: string;  // Different prop name
  onAction: (e: React.MouseEvent) =&amp;gt; Promise&amp;lt;void&amp;gt;;  // Different type
}

const BadPrimaryButton: React.FC&amp;lt;BadPrimaryButton&amp;gt; = ({ label, onAction }) =&amp;gt; {
  const handleClick = async (e: React.MouseEvent) =&amp;gt; {
    await onAction(e);
  };

  return &amp;lt;button onClick={handleClick}&amp;gt;{label.toUpperCase()}&amp;lt;/button&amp;gt;;
};

// Usage shows incompatibility
const BadExample: React.FC = () =&amp;gt; {
  const handleClick = () =&amp;gt; console.log(&amp;#x27;clicked&amp;#x27;);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;BadBaseButton
        onClick={handleClick}
        text=&amp;quot;Click me&amp;quot;
      /&amp;gt;

      {/* Won&amp;#x27;t work! Different props and behavior */}
      &amp;lt;BadPrimaryButton
        label=&amp;quot;Click me&amp;quot;
        onAction={async (e) =&amp;gt; console.log(&amp;#x27;clicked&amp;#x27;)}
      /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
&lt;/pre&gt;
  &lt;p id=&quot;iVYR&quot;&gt;&lt;strong&gt;Following LSP:&lt;/strong&gt; It helps to ensure ****base props are extended consistently, the same onClick type throughout, components are interchangeable &amp;amp; add features without breaking the base contract.&lt;/p&gt;
  &lt;pre id=&quot;RFYZ&quot;&gt;// ✅ GOOD EXAMPLE - Following LSP
interface GoodBaseButtonProps {
  onClick: () =&amp;gt; void;
  children: React.ReactNode;
}

const GoodBaseButton: React.FC&amp;lt;GoodBaseButtonProps&amp;gt; = ({ onClick, children }) =&amp;gt; (
  &amp;lt;button onClick={onClick}&amp;gt;{children}&amp;lt;/button&amp;gt;
);

// Properly extends base props
interface GoodPrimaryButtonProps extends GoodBaseButtonProps {
  variant?: &amp;#x27;solid&amp;#x27; | &amp;#x27;outline&amp;#x27;;  // Optional additional prop
}

const GoodPrimaryButton: React.FC&amp;lt;GoodPrimaryButtonProps&amp;gt; = ({
  onClick,
  children,
  variant = &amp;#x27;solid&amp;#x27;
}) =&amp;gt; {
  const className = variant === &amp;#x27;solid&amp;#x27; ? &amp;#x27;bg-blue-500&amp;#x27; : &amp;#x27;border-blue-500&amp;#x27;;

  return (
    &amp;lt;button onClick={onClick} className={className}&amp;gt;
      {children}
    &amp;lt;/button&amp;gt;
  );
};

// Usage shows compatibility
const GoodExample: React.FC = () =&amp;gt; {
  const handleClick = () =&amp;gt; console.log(&amp;#x27;clicked&amp;#x27;);

  return (
    &amp;lt;div&amp;gt;
      {/* Both components can use the same handler */}
      &amp;lt;GoodBaseButton onClick={handleClick}&amp;gt;
        Click me
      &amp;lt;/GoodBaseButton&amp;gt;

      &amp;lt;GoodPrimaryButton onClick={handleClick} variant=&amp;quot;solid&amp;quot;&amp;gt;
        Click me
      &amp;lt;/GoodPrimaryButton&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
&lt;/pre&gt;
  &lt;p id=&quot;nQvS&quot;&gt;x&lt;/p&gt;

</content></entry></feed>