typescript define object type code example

Example 1: type of object python

>>> type([]) is list
True
>>> type({}) is dict
True
>>> type('') is str
True
>>> type(0) is int
True

Example 2: define object properties typescript

interface ISomeObject {
  subPropertie1: string,
  subPropertie2: number,
}

interface IProperties {
  property1: string,
  property2: boolean,
  property3: number[],
  property4: ISomeObject,
  property5: ISomeObject[],
}

function (args:IProperties): void {		// Sample Usage
  console.log(args.property1);
}

Example 3: typescript object type

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

Example 4: typescript string in object property

protected get ButtonClass(): object {
    const buttonClass = {
      'cursor-pointer hover:shadow focus:shadow': this.Enabled,
      'opacity-40 cursor-not-allowed': !this.Enabled,
      'whitespace-no-wrap': !this.LineBreaks
    }
    // The below allows you to define object properties based on a variable.
    // If you were to assign them in the snippet above, it would cause an error.
    buttonClass[`hover:${this.Color.FocusColorClass}`] = this.Enabled;
    buttonClass[`focus:${this.Color.FocusColorClass}`] = this.Enabled;
    buttonClass[`active:${this.Color.ActiveColorClass}`] = this.Enabled;
    return buttonClass;
  }

Example 5: angular type of string

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