-1

I have tried to reassign the property type to string, but if I reassign the property "type" to string, TypeScript throws an error

Type 'string' is not assignable to type '{ title: { value: string; }; }'.

const temp= {
  type: {
      title: {
          value: "text"
      }
  }
}

temp.type=temp.type.title.value

a screenshot of what I tried

5
  • Why do you need to do this? Please may you share the issue you are trying to solve with this? Commented Jul 13, 2022 at 17:37
  • 1
    Hello, it would be helpful to share what you're trying to achieve by doing this. As such, this is invalid in TS. Commented Jul 13, 2022 at 17:39
  • 1
    Please do not upload images of code/data/errors when asking a question. Commented Jul 13, 2022 at 17:39
  • The answer to what? How to change the type of a property? Create a new object with a property of the same name with a new type, or use a type union (i.e., Typescript Allow class property to have multiple types) Commented Jul 13, 2022 at 17:48
  • 3
    The type of temp.type is { title: { value: string } }. You can't change it to just string, that defeats the whole point of a type system. Make a new, different object that is of the correct type where it's type property is just a string. On a related note, PLEASE don't use the word 'type' as an object property (or variable name) when asking Typescript questions: it creates confusion about whether you understand the difference between the runtime value of the object and the compile-time type of the object, and askers are essentially never given the benefit of the doubt. Commented Jul 13, 2022 at 18:03

3 Answers 3

3

As Jared Smith wrote in the comment

The type of temp.type is { title: { value: string } }. You can't change it to just string, that defeats the whole point of a type system. Make a new, different object that is of the correct type where it's type property is just a string

Just to add more to this. You can create a type that allows reassign like this:

type Temp = {
  type: string | {
    title: {
      value: string;
    }
  }
}
const temp: Temp = {
  type: {
      title: {
          value: "text"
      }
  }
}

But there is probably no reason to do this.

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

Comments

0

typescript prohibits doing that. You need to rethinking your task, because you are doing something wrong.

As says official site "TypeScript is a strongly typed programming language...". among other things it means variable is associated with the type at the time of declaration and the type cannot be changed later.

When you write

let foo = {
  title: {
    value: "text"
  }
};

typescript now knows that foo type is object that consists of one property "title" with value type object that consists of one property "value" with value type string.

type T = {
  title: {
    value: string
  }
}

When you write

foo = "string"

typescript compares two types T and string. Type T is not string. Here you got the type error.

1 Comment

This answer, while true, isn't very helpful. Could you expand on it with an explanation of why it would be a bad idea for Typescript to allow that?
0

Since you did not define a type for this object, Typescript has inferred the type and prevented you from changing it. If you want to be able to change that property's type to anything, you can declare it as type any.

const temp: { type: any } = {
  type: {
      title: {
          value: "text"
      }
  }
}

temp.type = "text";

Comments

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.