Skip to content

Instantly share code, notes, and snippets.

@marsen
Created September 30, 2024 05:43
Show Gist options
  • Save marsen/9b6f041177d736f36c42a372ff684f66 to your computer and use it in GitHub Desktop.
Save marsen/9b6f041177d736f36c42a372ff684f66 to your computer and use it in GitHub Desktop.
Marsen's Blog typescript_string_intersection_trick
import { HeroProps } from "@/types/HeroProps";
export const Hero = ({ race,name }: HeroProps) => {
return (
<h1>Hero: {name} is {race}</h1>
);
};
// src/components/HeroDisplay.tsx
import React from 'react';
import { Hero } from '../components/hero'; // 引入 Hero 型別
const HeroDisplay = () => {
return (
<div>
<Hero name="alice" race="human" />
<Hero name="mark" race="demon" />
<Hero name="grace" race="" />
</div>
);
};
export default HeroDisplay;
//type Race = 'human' | 'demon' | string;
type Race = 'human' | 'demon' | (string & {});
export type HeroProps = {
name: string;
race: Race;
}
@marsen
Copy link
Author

marsen commented Sep 30, 2024

React 專案資料結構
截圖 2024-09-30 下午1 48 07

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