Practicing Typescript: Template literals and Exclude

zenoan·2021년 12월 21일
0

typescript

목록 보기
8/11

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>
}
profile
프론트엔드 개발자

0개의 댓글