Created
September 30, 2024 05:43
-
-
Save marsen/9b6f041177d736f36c42a372ff684f66 to your computer and use it in GitHub Desktop.
Marsen's Blog typescript_string_intersection_trick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HeroProps } from "@/types/HeroProps"; | |
export const Hero = ({ race,name }: HeroProps) => { | |
return ( | |
<h1>Hero: {name} is {race}</h1> | |
); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//type Race = 'human' | 'demon' | string; | |
type Race = 'human' | 'demon' | (string & {}); | |
export type HeroProps = { | |
name: string; | |
race: Race; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
React 專案資料結構