#What's the best way to define custom types for a class?

5 messages · Page 1 of 1 (latest)

sacred zephyr
#

For example, I have a class that looks like this:

class CustomForm<S extends FormSchema> {
  schema: S;
  options: { unsetOnFail: string[] };

  constructor(schema: S, options: { unsetOnFail: string[] }) {
      this.schema = schema;
      this.options = options
  }

  // ...
}

I want to define the type for both the options argument in the constructor and the options private member in one place.

I know I could do something like type CustomFormOptions = { unsetOnFail: string[] } outside of the class. Is this the only or recommended way to do it in general? I feel like it would be nicer to define this somewhere inside the class with the type Options or something, since outside the class I have other things that use the name Options and it would make things more verbose to have to call it CustomFormOptions and rename the other type as well.

I wish I could do type Options = ... inside the class itself so it's scoped just to the class almost as if it were a member, but I don't think that's possible. Just checking if there's something like that or I'm doing it wrong.

Thank you all!

deft sun
#
class CustomForm<S extends FormSchema> {

  constructor(public schema: S, public options: { unsetOnFail: string[] }) {
      this.schema = schema;
      this.options = options
  }

  // ...
}
#

!hb parameter properties

surreal shellBOT
sacred zephyr
#

Awesome, thank you @deft sun!