Giter Site home page Giter Site logo

typescript's Introduction

TypeScript

Kurulum

npm install -g typescript

Downleveling

TypeScript varsayılan olarak ES3'e göre kodu çevirir. Eğer daha yüksek bir sürüm hedefliyorsak tsc --target es2015 hello.ts komutunu kullanabiliriz.

Güncel tarayıcıların büyük bir çoğunluğu ES6'yı desteklediği için varsayılan hedef bu sürüme yükseltilebilir.

Explicit Types

Değişkenlerin türlerininin belirtilmesi

function greet(person: string, date: Date) {
  console.log(`Hello ${person}, today is ${date.toDateString()}!`);
}

Tüm türler: string, number, boolean, number[], string[], any

Type Annotations on Variables

Değişkenleri tanımlarken değerini verebiliriz.

let myName: string = "Alice";

Return Type Annotations

Bir fonksiyonda return edilen değerin türünü belirtebiliriz.

function getFavoriteNumber(): number {
  return 26;
}

Optional Object Properties

Opsiyonel bir parametre var ise bunu ? ile belirtebiliriz.

function printName(obj: { first: string; last?: string }) {
  // ...
}

// Both OK
printName({ first: "Bob" });
printName({ first: "Alice", last: "Alisson" });

Union Types

Değişkenlere birden çok tür atanabilir.

function printId(id: number | string) {
  console.log("Your ID is: " + id);
}
// OK
printId(101);
// OK
printId("202");
// Error
printId({ myID: 22342 });

Bu şekilde birden çok tür atanan değişkenlerde işlem yaparken typeof ile tip kontrolü yapmalıyız.

function printId(id: number | string) {
  if (typeof id === "string") {
    // In this branch, id is of type 'string'
    console.log(id.toUpperCase());
  } else {
    // Here, id is of type 'number'
    console.log(id);
  }
}

Type Aliases

type Point = {
  x: number;
  y: number;
};
 
// Exactly the same as the earlier example
function printCoord(pt: Point) {
  console.log("The coordinate's x value is " + pt.x);
  console.log("The coordinate's y value is " + pt.y);
}
 
printCoord({ x: 100, y: 100 });
type ID = number | string;

type yerine interfacete kullanılabilir (Tamamen aynı şey değildir fakat büyük ölçüde benzerdir):

interface Point {
  x: number;
  y: number;
}

Literal Types

function printText(s: string, alignment: "left" | "right" | "center") {
  // ...
}
printText("Hello, world", "left");
printText("G'day, mate", "centre"); // Hata
function compare(a: string, b: string): -1 | 0 | 1 {
  return a === b ? 0 : a > b ? 1 : -1;
}
interface Options {
  width: number;
}
function configure(x: Options | "auto") {
  // ...
}
configure({ width: 100 });
configure("auto");
configure("automatic"); // Hata

Discriminated unions

interface Shape {
  kind: "circle" | "square";
  radius?: number;
  sideLength?: number;
}

function handleShape(shape: Shape) {
  // ...
}
interface Circle {
  kind: "circle";
  radius: number;
}
 
interface Square {
  kind: "square";
  sideLength: number;
}
 
type Shape = Circle | Square;

Object Types

function greet(person: { name: string; age: number }) {
  return "Hello " + person.name;
}
type Person = {
  name: string;
  age: number;
};
 
function greet(person: Person) {
  return "Hello " + person.name;
}

typescript's People

Contributors

omergulcicek avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.