Alexandr Kalabin
Alexandr Kalabin

Reputation: 1

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> {}
}

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.

UPD

This is what worked out for me. Based on this solution TypeScript interface with array destructuring

import { Validation } from './validation';

export interface ValidationProfile extends Iterable<HTMLFormElement | Validation> {
    form: HTMLFormElement;
    validation: Validation;
    [0]: HTMLFormElement;
    [1]: Validation;
}

Upvotes: 0

Views: 43

Answers (0)

Related Questions