如何获取数组项中嵌套属性的映射类型?

时间:2019-07-03 16:17:31

标签: typescript

const arr = [
  { key: 'one', value: '1' },
  { key: 'two', value: '2' },
  { key: 'three', value: '3' },
] as const;

type Keys = GetKeys<typeof arr>;

如何定义GetKeys使得Keys'one' | 'two' | 'three'

1 个答案:

答案 0 :(得分:1)

您只想几次使用lookup type(也称为“索引访问类型”)从数组中获取数字键元素,然后从这些键的public async Task Play(CancellationToken cancel) { if (State == PlaybackState.Playing) return; if (RemainingAirTime == null) RemainingAirTime = ETimeSpan.FromSeconds(Display.Settings.ScrollTime); if (cancel.IsCancellationRequested) return; _ct = new Countdown(RemainingAirTime.BaseSpan); _ct.Elapsed += _ct_Elapsed; _ct.Start(); try { Draw(); }catch(System.Exception ex) { Blenet.Exceptions.ExceptionHandler.HandleException(ex, "Error while drawing", this); } State = PlaybackState.Playing; await Task.Delay(RemainingAirTime.BaseSpan, cancel); if(State == PlaybackState.Playing) await Reset(); } private void _ct_Elapsed(TimeSpan obj) { if (State == PlaybackState.Playing) RemainingAirTime = new ETimeSpan(obj); } 键属性中获取:

"key"

希望有所帮助;祝你好运!

Link to code