Practicing Typescript: Template literals and Exclude
5294 단어 typescript타입스크립트typescript
Use of template literals and Exclude keyword
- Using tempalte literals to create many strings by making use of unions.
import { Toast } from './components/templateliterals/Toast'
function App() {
return (
<div className='App'>
<Toast position='center' />
</div>
)
}
// Toast.tsx
// Toast notification component with position properties:
type HorizontalPosition = 'left' | 'center' | 'right'
type VerticalPosition = 'top' | 'center' | 'bottom'
// Combine horizontal position and vertical position
// Center-center combination is preferred as just center, therefore:
// Use Exclude keyword: Exclude<Types, type wanted to be excluded>
type ToastProps = {
position:
| Exclude<`${HorizontalPosition}-${VerticalPosition}`, 'center-center'>
| 'center'
}
/*
* Position prop can be one of
* "left-center" | "left-top" | "left-bottom" | "center" | "center-top" |
* "center-bottom" | "right-center" | "right-top" | "right-bottom"
*/
export const Toast = ({ position }: ToastProps) => {
return <div>Toast Notification Position - {position}</div>
}
Author And Source
이 문제에 관하여(Practicing Typescript: Template literals and Exclude), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@jha0402/Practicing-Typescript-Template-literals-and-Exclude저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)