0

How can I convert the code from Javascript to typescript

const ORIENTATIONS = {
  single: () => 0,
  'right angled': (tag) => {
    return hashWithinRange(tag.text) * 90;
  },
  multiple: (tag) => {
    return hashWithinRange(tag.text) * 15 - 90;
  },
};

And I'm getting this error:

 const ORIENTATIONS: {
    single: () => number;
    'right angled': (tag: any) => number;
    multiple: (tag: any) => number;
}

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ single: () => number; 'right angled': (tag: any) => number; multiple: (tag: any) => number; }'. No index signature with a parameter of type 'string' was found on type '{ single: () => number; 'right angled': (tag: any) => number; multiple: (tag: any) => number; }'.ts(7053)

4
  • What issues are you having while converting it? Commented Apr 27, 2020 at 5:44
  • 3
    actually, any JavaScript code is also technically valid TypeScript code (even though, by default, compiler could complain). What is your actual error? Commented Apr 27, 2020 at 5:44
  • 1
    It already is TypeScript. Commented Apr 27, 2020 at 5:44
  • related : Is any JavaScript code a valid TypeScript code? Commented Apr 27, 2020 at 5:46

1 Answer 1

0

You can create one class as

export class AppComponent {
  name = "Angular";

  ORIENTATIONS = {
    single: () => 0,
    "right angled": tag => {
      return this.hashWithinRange(tag.text) * 90;
    },
    multiple: tag => {
      return this.hashWithinRange(tag.text) * 15 - 90;
    }
  };

  hashWithinRange(value) {
    return 45;
  }
}

Demo https://stackblitz.com/edit/angular-qm5wdc

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.