3

I'm trying to implement a method like that takes a key argument that is either a string or an instance of the indexable type interface IValidationContextIndex. The implementation looks like this:

  /**
   * Gets all ValidationContext container values.
   * @returns An array of ValidationContext instances contained in the cache.
   */
  public static getValidationContextValues(key: IValidationContextIndex | string ): Array<ValidationContext> {
    if (key instanceof IValidationContextIndex ) [
      return Object.values(<any> key);
    ]
    else {
      const vci = ValidationContainer.cache[<string>key];
      return Object.values(<any> vci);
    }
  }  

Typescript give the following error for the if block:

[ts] 'IValidationContextIndex' only refers to a type, but is being used as a value here.

Any ideas on how to fix this?

For most interface I think it's possible to add a type property ( type: 'IValidationContextsIndex'; ), but that does not work in this case since the the interface is an indexable type interface ....

6
  • You can use any as type Commented Jun 28, 2018 at 3:45
  • If I do key: any and perform an instanceof check I still get the same error ... Commented Jun 28, 2018 at 3:47
  • Remember that interfaces and types just disappear once you transpile into javascript. Those are for your code-checking and linting. Not for your end-result. Commented Jun 28, 2018 at 3:47
  • Yup I understand that part - basically the interface is gone, so Typescript is like "Dude I got nottin ..." Commented Jun 28, 2018 at 3:48
  • Maybe this will help then: stackoverflow.com/a/40718205/1497533 Commented Jun 28, 2018 at 3:50

3 Answers 3

2

There isnt a way to check the type in typescript at run time as almost everything becomes an object once transpiled, so you may have to something along what is defined as user-defined typed guards

Sign up to request clarification or add additional context in comments.

Comments

2

I think this will do it (Per the tip from @indrakumara):

    /**
     * Gets all ValidationContext container values fpr a 
     * Object_property string valued key or a key that is an instance of 
     * IValidationContextIndex).
     * @returns An array of ValidationContext instances contained in the cache that corresponds to a specific Object_property key.
     */
    public static getValidationContextValues(key: any ): Array<ValidationContext> {
      if (typeof key === `string` ) {
        const vci = ValidationContainer.cache[<string>key];
        return Object.values(<any> vci);
      }
      else {
        return Object.values(<IValidationContextIndex> key);
      }
    }  

Comments

1

Interfaces in typescript doesn't transpile into any code in javascript. So in your code, "instanceof IValidationContextIndex", IValidationContextIndex doesn't exist in javascript.

Change your interface to class or have a class implementing the interface and then check if passed parameter is instanceof that class.

1 Comment

It was a little easier to just check whether the instance is a string, and if not assume that is an instance of the interface ... So I cheated a little, but it's lightweight and works ...I think your way would be robust when the union type is composed of multiple interfaces ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.