Describe only shape of an object in typescript, but ignore the types

Solution 1:

As a concept, I do not see advantage of declaring something to be boolean and assigning it as a string...

But we can adjust it like this:

//type ShapeType<TShape extends Shape> = TShape;
interface Shape { [fieldName: string]: any };

// now, TestInterface also contains [fieldName: string]
interface TestInterface extends Shape {
  TestOne?: string;
  TestTwo?: number;
  TestThree?: boolean;
}


type ShapeType<TShape extends Shape> = TShape;

// BTW - why we declared TestsThree to be boolean
// if we assign it to string... that would hardly help
var test: ShapeType<TestInterface> = {
    TestsThree: "asdf",
}

Or, if we do not want to have a Shape to be interface,

// we have to do this explicitly [fieldName: string]
interface TestInterface {
  [fieldName: string]: any 
  TestOne?: string;
  TestTwo?: number;
  TestThree?: boolean;
}

type Shape = { [fieldName: string]: any };

type ShapeType<TShape extends Shape> = TShape;

var test: ShapeType<TestInterface> = {
    TestsThree: "asdf",
}

Both approaches will work, but again.. Why we would define TestThree?: boolean; and then assign it as TestsThree: "asdf",?