0

Like if there is an interface

interface IHello {
  x: 'hello';
}

function fun(obj: IHello) {
  ...
}

then doing this

var obj = {
  x: 'hello'
};

fun(obj);

gives an error Type string is not assignable to type hello;

What is the correct way of doing it?

  • The inferred type is `{ x: string }`, do e.g. `var obj: IHello = {...}`. Note that the term is [literal type](https://www.typescriptlang.org/docs/handbook/advanced-types.html#string-literal-types). – jonrsharpe Jun 17 '20 at 16:44
  • You could also do `var obj = {x: 'hello'} as const;` for a narrower inference (which I think gives you the type `{readonly x: "hello"}`. – jcalz Jun 17 '20 at 16:47

0 Answers0