typescript type object code example

Example 1: typescript type object

type Dictionary = {
  [key: string]: any
}

Example 2: typescript object type

//is not strict mode
let endedCoord: {x: number, y: number} = {
  	x: -1,
  	y: -1,
}

Example 3: angular type of string

if(typeof myVariable === 'string'){
	//do
}

Example 4: typescript type of object values

const data = {
  value: 123,
  text: 'text'
};
type Data = typeof data;

Example 5: typescript operate with html objects

ts// 1. Select the div element using the id property
const app = document.getElementById("app");

// 2. Create a new <p></p> element programmatically
const p = document.createElement("p");

// 3. Add the text content
p.textContent = "Hello, World!";

// 4. Append the p element to the div element
app?.appendChild(p);

Example 6: object type in typescript

declare function create(o: object | null): void;
// OKcreate({ prop: 0 });create(null);create(undefined); // with `--strictNullChecks` flag enabled, undefined is not a subtype of nullArgument of type 'undefined' is not assignable to parameter of type 'object | null'.2345Argument of type 'undefined' is not assignable to parameter of type 'object | null'.
create(42);Argument of type '42' is not assignable to parameter of type 'object | null'.2345Argument of type '42' is not assignable to parameter of type 'object | null'.create("string");Argument of type '"string"' is not assignable to parameter of type 'object | null'.2345Argument of type '"string"' is not assignable to parameter of type 'object | null'.create(false);Argument of type 'false' is not assignable to parameter of type 'object | null'.2345Argument of type 'false' is not assignable to parameter of type 'object | null'.Try

Tags: