Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generator a union that can suport time format type #13015

Open
wqs576222103 opened this issue Jul 15, 2022 · 2 comments · May be fixed by #13016
Open

Generator a union that can suport time format type #13015

wqs576222103 opened this issue Jul 15, 2022 · 2 comments · May be fixed by #13016
Labels
new-challenge Propose a new challenge, a PR will be auto generated

Comments

@wqs576222103
Copy link

Please follow the template and fill the info. A PR will be auto-generated and always reflect on your changes.

Detailed solution/guide is not required, but please be sure the challenge is solvable.

Info

Basic info of your challenge questions,

difficulty: medium # medium / hard / extreme
title: TimeUnion
# tags: union, tuple # separate by comma

Question

Generator a union that can suport time format type

For example, the function component named 'timePicker' with time params like value、onChange. we need a union type like
'YYYY-MM-DD HH:mm:ss'|'YYYY-MM-DD'|'HH:mm:ss'|'YYYY-MM HH:ss'|... not string, but can be ""

Template

This is the template for challengers to start the coding. Basically, you just need to change the name of your generic/function and leave to implementation any.

type TimeUnion = any

Test Cases

Provide some test cases for your challenge, you can use some utils from @type-challenges/utils for asserting.

import type { Equal, Expect } from '@type-challenges/utils'

type Has<T extends string, U extends string> =  U extends T ? true : false

type cases = [
  Expect<Equal<Has<TimeUnion, 'YYYY-MM-DD HH:mm:ss'>, true>>,
  Expect<Equal<Has<TimeUnion, 'YYYY-MM-DD'>, true>>,
  Expect<Equal<Has<TimeUnion, 'HH:mm:ss'>, true>>,
  Expect<Equal<Has<TimeUnion, 'YYYY-DD HH:mm:ss'>, true>>,
  Expect<Equal<Has<TimeUnion, 'YYYY-MM HH:mm:ss'>, true>>,
  Expect<Equal<Has<TimeUnion, 'YYYY HH:mm:ss'>, true>>,
  Expect<Equal<Has<TimeUnion, 'YYYY-MM-DD HH:ss'>, true>>,
] 
@wqs576222103 wqs576222103 added the new-challenge Propose a new challenge, a PR will be auto generated label Jul 15, 2022
@github-actions github-actions bot linked a pull request Jul 15, 2022 that will close this issue
github-actions bot pushed a commit that referenced this issue Jul 15, 2022
@github-actions
Copy link
Contributor

#13016 - Pull Request updated.

2022-07-15T02:59:53.070Z Preview in Playground

@wqs576222103
Copy link
Author

// 时间格式的定义
type TTimeSeparators = {
YYYY: '-'
MM: '-'
DD: ' '
HH: ':'
mm: ':'
ss: ''
'': ''
}

type TTimeTuple = ['YYYY', 'MM', 'DD', 'HH', 'mm', 'ss', '']

type TSeparators<T, Separators extends string> = T extends T
? T extends ''
? ''
: ${T & string}${Separators}
: never

declare type TTimeFormat<
T extends unknown[] = TTimeTuple,
R extends string = '',
Separators extends string = ''> = T extends [infer F, ...infer Rest]
? TTimeFormat<
Rest,
R | ${TSeparators<R, Separators>}${F & string},
F extends keyof TTimeSeparators ? TTimeSeparators[F] : ''
>
: R

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-challenge Propose a new challenge, a PR will be auto generated
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant