javascript – How to declare in a *.d.ts file an Iterable of two different types?


This is the javascript function I’m unsuccessfully trying to make vscode show autocomplete suggestions for the returned object:

const profile = (form, validation) =>
  Object.defineProperties(
    { form, validation },
    {
      0: { value: form },
      1: { value: validation },
      length: { value: 2 },
      [Symbol.iterator]: { value: Array.prototype[Symbol.iterator] },
      [Symbol.toStringTag]: { value: 'ValidationProfile' },
    },
  );

This is the *d.ts file for the returned object’s type I was experimenting with. It’s definitely wrong.:

import { Validation } from './validation';

export type ValidationProfile = typeof ValidationProfileAPI;

// !!! the returned object is not an array, just tried to make it work somehow
const ValidationProfileAPI: [HTMLFormElement, Validation];

declare namespace ValidationProfileAPI {
    let form: HTMLFormElement;
    let validation: Validation;
    interface iterator implements Iterable<HTMLFormElement|Validation> {}
}

Usage:

// shows available property propA for a and propB for b
const {a, b} = profile({propA: 'a'}, {propB: 'b'}); 

// doesn't show available properties for a and for b
const [a, b] = profile({propA: 'a'}, {propB: 'b'}); 

I managed to find a similar problem here but I can’t figure out how to adapt it for my case.
I will be grateful for any suggestions.



Source link

Leave a Comment