如何将对象的键限制为打字稿中设置的字符串列表

时间:2019-08-14 23:53:06

标签: typescript

我有一个api可以返回以下其中一项:

{ fill: 'string'}{stroke: 'string'}{effect: 'string'}

我的密钥类型如下:

type StyleKeyType =
  | 'fill'
  | 'stroke'
  | 'effect';

,我想为该对象创建一个类型。我尝试过:

type StylesObject = { [K in StyleKeyType]?: string };

但这失败了,因为它认为{}是有效的,在我看来不是。

有没有一种方法可以生成这种类型,而无需手动拼出每种可能的对象类型?

1 个答案:

答案 0 :(得分:1)

select dd.full_date, case when month(dd.full_date) = 1 then case -- New Year when day(dd.full_date) = 1 and date_format(dd.full_date ,'u') in (1,2,3,4,5) then 'NEW_YEAR' -- If new year is a Saturday or Sunday, then I have used the immediate Monday as the holiday. when day(dd.full_date) = 2 and date_format(dd.full_date ,'u') = 1 then 'NEW_YEAR' when day(dd.full_date) = 3 and date_format(dd.full_date ,'u') = 1 then 'NEW_YEAR' -- MLK Day. 3rd Monday of January when day(dd.full_date) in (15,16,17,18,19,20,21) and date_format(dd.full_date,'u') = 1 then 'MLK' when date_format(dd.full_date,'u') in (6) then 'WE_SAT' when date_format(dd.full_date,'u') in (7) then 'WE_SUN' else 'WD' end when month(dd.full_date) = 2 then case -- President's Day. 3rd Monday of February when day(dd.full_date) in (15,16,17,18,19,20,21) and date_format(dd.full_date,'u') = 1 then 'PRES_DAY' when date_format(dd.full_date,'u') in (6) then 'WE_SAT' when date_format(dd.full_date,'u') in (7) then 'WE_SUN' else 'WD' end when month(dd.full_date) = 5 then case -- Memorial Day. Last Monday of May when day(dd.full_date) in (25,26,27,28,29,30,31) and date_format(dd.full_date,'u') = 1 then 'MEMORIAL' when date_format(dd.full_date,'u') in (6) then 'WE_SAT' when date_format(dd.full_date,'u') in (7) then 'WE_SUN' else 'WD' end when month(dd.full_date) = 7 then case -- July 4th. when day(dd.full_date) = 4 and date_format(dd.full_date ,'u') in (1,2,3,4,5) then 'JULY_4' -- If July 4th is a Sunday then July 5th (Monday) is considered a holiday. when day(dd.full_date) = 5 and date_format(dd.full_date ,'u') = 1 then 'JULY_4' -- If July 4th is a Saturday then July 3rd (Friday) is considered a holiday. when day(dd.full_date) = 3 and date_format(dd.full_date ,'u') = 5 then 'JULY_4' when date_format(dd.full_date,'u') in (6) then 'WE_SAT' when date_format(dd.full_date,'u') in (7) then 'WE_SUN' else 'WD' end when month(dd.full_date) = 9 then case -- Labor Day. First Monday of September when day(dd.full_date) in (1,2,3,4,5,6,7) and date_format(dd.full_date,'u') = 1 then 'LABOR' when date_format(dd.full_date,'u') in (6) then 'WE_SAT' when date_format(dd.full_date,'u') in (7) then 'WE_SUN' else 'WD' end when month(dd.full_date) = 10 then case -- Columbus Day. 2nd Monday of October when day(dd.full_date) in (8,9,10,11,12,13,14) and date_format(dd.full_date,'u') = 1 then 'COLUMBUS_DAY' when date_format(dd.full_date,'u') in (6) then 'WE_SAT' when date_format(dd.full_date,'u') in (7) then 'WE_SUN' else 'WD' end when month(dd.full_date) = 11 then case -- Thanksgiving. 4th Thursday of November when day(dd.full_date) in (22,23,24,25,26,27,28) and date_format(dd.full_date,'u') = 4 then 'THANKSGIVING' when date_format(dd.full_date,'u') in (6) then 'WE_SAT' when date_format(dd.full_date,'u') in (7) then 'WE_SUN' else 'WD' end when month(dd.full_date) = 12 then case -- CHRISTMAS when day(dd.full_date) = 25 and date_format(dd.full_date ,'u') in (1,2,3,4,5) then 'CHRISTMAS' -- If 25th Dec is a Sunday when day(dd.full_date) = 26 and date_format(dd.full_date ,'u') = 1 then 'CHRISTMAS' -- If 25th Dec is a Saturday when day(dd.full_date) = 24 and date_format(dd.full_date ,'u') = 5 then 'CHRISTMAS' when date_format(dd.full_date,'u') in (6) then 'WE_SAT' when date_format(dd.full_date,'u') in (7) then 'WE_SUN' else 'WD' end when month(dd.full_date) in (3,4,6,8,10) then case when date_format(dd.full_date,'u') in (6) then 'WE_SAT' when date_format(dd.full_date,'u') in (7) then 'WE_SUN' else 'WD' end end as us_federal_holiday from date_dim dd 有效,因为{}已将其所有属性标记为可选的StylesObject

以下是一个对您有用的选项:

?