Skip to content

Instantly share code, notes, and snippets.

@canavci2016
Last active September 9, 2022 21:46
Show Gist options
  • Save canavci2016/044e89778094ec18ac7567e1b65cbb53 to your computer and use it in GitHub Desktop.
Save canavci2016/044e89778094ec18ac7567e1b65cbb53 to your computer and use it in GitHub Desktop.
type script boilerplate
interface User {
name: string;
id: number;
}
class UserAccount implements User {
name: string;
id: number;
constructor(name: string, id: number) {
this.name = name;
this.id = id;
}
}
const user: User = new UserAccount("Murphy", 2);
function getAdminUser(): User {
const user: User = new UserAccount("can avci", 1);
return user;
}
type WindowStates= "open" | "closed" | "minimized";
function runtWindow(state:WindowStates):void
{
console.log(state);
}
runtWindow("closed");
function getLength(obj:string|string[]):number{
return obj.length;
}
type ObjectWithNameArray=Array<{name:string}>
interface BackPack<Type>
{
add:(obj:Type)=>void;
get:()=>Type;
}
interface IPoint{
x:number;
y:number;
}
function logPoint(p:IPoint)
{
console.log(p);
}
class VirtualPoint
{
public x:number;
public y:number;
constructor(x:number,y:number)
{
this.x=x;
this.y=y;
}
}
logPoint({x:23,y:42});
const pointInstance:IPoint=new VirtualPoint(32,52);
logPoint(pointInstance);
const announcement = "can avc";
announcement.toLocaleLowerCase();
const str:string=`hello ${announcement}`;
let obj2:any={name:"cad"};
obj2.mm;
let myName="can abc";
myName="awdawd";
function printCord(pt:{x:number,y:number,z?:number})
{
console.log("The coordinate's x value is " + pt.x);
console.log("The coordinate's y value is " + pt.y);
if(pt.z!==undefined)
{
console.log("The coordinate's z value is " + pt.z);
}
}
printCord(pointInstance);
function printId(id:number | string)
{
console.log("Your ID is: " + id);
}
printId(23);
printId("Merhaba");
function welcomePeople(x:string | Array<string>)
{
if(Array.isArray(x))
console.log("Hello, " + x.join(" and "));
else
console.log("Welcome lone traveler " + x);
}
type Animal={name:string};
type Bear=Animal & {honey:boolean};
const bearValue:Bear={name:"cc",honey:true};
const myCanvas=document.getElementById("main_canvas") as HTMLCanvasElement;
function printText(s:string,alligment:"left" | "right" | "center"){
}
printText("Hello, world", "left");
printText("merhaba","center");
interface Options{
width:number;
}
function configure(x: Options | "auto") {
// ...
}
configure({width:250});
configure("auto");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment