How do you declare the type a property of an object in-place, without needing to declare the type of the entire object?

RobertAKARobin

Is there a way to type a property of an object as you’re writing the object? As opposed to defining a type/interface beforehand.

The only thing I can think of is as but that’s too lenient. Consider:

type Comment = Required<{
    author: string;
    message: string;
}>;

const myComment: Comment = {
    author: 'Steve',
    // Error! :) 'message' is missing
};

const myData = {
    myComment: {
        author: 'Steve',
        // No error. :(
    } as Comment,
};

const myData = {
    myComment: <Comment>{
        author: 'Steve',
        // No error. :(
    },
};

I want myData.myComment to throw the same error.

This is a trivial example, of course. But if I'm writing a big complex object it would be nice to be able to declare types for portions of it on-the-fly.

jsejcksn

Note: Comment is a built-in type in lib.dom.d.ts. If you use that identifier with the dom library, you'll get this error:

Duplicate identifier 'Comment'.(2300)

You can use an identity function to ensure that the input is assignable to the parameter type:

TS Playground

type C = {
  author: string;
  message: string;
};

/** If you use this, you MUST provide the generic type argument to constrian your value */
function id <T>(value: T): T {
  return value;
}

const number1 = id<number>('hello'); /*
                           ^^^^^^^
Argument of type 'string' is not assignable to parameter of type 'number'.(2345)
*/

// Don't forget to supply the generic type argument!
const number2 = id('hello'); // typeof value2 === "hello"

const myData1 = {
  myComment: id<C>({
    author: 'Steve',
    // message: 'Hi',
  }), /*
^^^^^^^^^^^^^^^^^^^^^^^^^
Argument of type '{ author: string; }' is not assignable to parameter of type 'C'.
  Property 'message' is missing in type '{ author: string; }' but required in type 'C'.(2345) */
};

const myData2 = {
  myComment: id<C>({
    author: 'Alex',
    message: 'Hello',
  }), // ok
};

console.log(myData1); // { myComment: { author: 'Steve' } }
console.log(myData2); // { myComment: { author: 'Alex', message: 'Hello' } }

Alternatively, if you're simply trying to avoid typing, you can define the comment ahead of the object in which you want to include it, like this:

TS Playground

type C = {
  author: string;
  message: string;
};

let myComment: C = {
  author: 'Steve',
  message: 'Hi',
};

const myData1 = { myComment };

myComment = {
  author: 'Alex',
  message: 'Hello',
};

const myData2 = { myComment };

console.log(myData1); // { myComment: { author: 'Steve', message: 'Hi' } }
console.log(myData2); // { myComment: { author: 'Alex', message: 'Hello' } }
console.log(myComment); // { author: 'Alex', message: 'Hello' }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you declare an object array in Java?

How to declare object of type class with multiple bounds

How to declare iterable object of another interface type?

Can you declare a object literal type that allows unknown properties in typescript?

How to declare an object of generic abstract class without specifying template type

TS(2352) Declare object with dynamic properties and one property with specific type

How do you declare a type alias in a f# constructor?

Python GraphQL How to declare a self-referencing graphene object type

Object or Boolean how to declare type in Typescript

Do you have to declare a function's type?

Declare type of a object key

Declare type object in an oracle package

How to declare an ArrayList of specified type using Class object

Why do people declare a variable of a base type of the object they assign to it?

how to declare type of function property returned on object

How can I declare the Type of a Variable in a Julia Object?

Declare object type in class

How to declare a variable or Object of any class type in Java

How to declare a boolean OR object type?

How to declare type of an object created dinamicaly by for await in TypeScript

Typescript: How to declare a type of array whose values are equal to keys of object?

How do you declare a generic type of an expression in a `requires` constraint?

How to declare an object of arrays of custom type in typescript

typescript: how to declare function rturn type that inside of object

How do I explicitly declare the type of the variable as object in Haxe?

How to declare a type from an object literal in typescript?

How to declare a type of (string, IDictionary<string, object>)

How do you declare that a type belongs to a marker interface?

How to declare a generic function in TypeScript to have only one property of an object T to be of a specific type?