Coder Social home page Coder Social logo

codermungan / learn-typescript Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 37 KB

A repo containing my notes and codes I took while learning Typescript.

TypeScript 55.27% JavaScript 24.32% HTML 20.41%
typescript typescript-learning typescript-learning-begginer

learn-typescript's Introduction

Başlangıç

Typescript Compiler aşamasında hatalarını gösterir. Compiler aşamasında çalışır.

Kurulum

npm install -G typescript

şeklinde TypeScript bilgisayarınıza kurulur.

Type Inference Özelliği

TypeScript eğer ki key’e herhangi bir type ataması yapmadıysanız, typescript keyin ilk value type’ını baz alacaktır. Örn;

let codermungan = {
	isim : "Mehmet Halil",
	soyisim : "MUNGAN",
	yas : 30,
};

Burada isim keyi string , soyisim keyi string , yas keyi number alacaktır.

Manuel type verme

Gene yukarıdaki örnekten gidecek olursak objenin içerisinde bulunan keylerinin typelarını ayarlamamız gerekmektedir. Örn;

let codermungan: {isim: string, soyisim: string, yas: number} = {
	isim : "Mehmet Halil",
	soyisim : "MUNGAN",
	yas : 30,
};

ancak daha okunaklı ve güzel bir syntax ile yazmamız gerekirse interface özelliğini yada type özelliğini kullanabiliriz. Örn;

// Objelerin type'ını oluştururken kullanımı daha yaygındır.
interface Coder {
	isim: string,
	soyisim: string,
	yas: number,
};

// Fonksiyon typelarını oluştururken kullanımı daha yaygındır.
type Coder = {
	isim: string,
	soyisim: string,
	yas: number,
};

let codermungan : Coder = {
	isim : "Mehmet Halil",
	soyisim : "MUNGAN",
	yas : 30,
};

!!! Dikkat

  • Objelerde type’lama yaptığınız key eğer ki objenizde kullanılmayacaksa hata verecektir. Çünkü inference özelliği onun direk type’nı variable olarak tanımladı. Ancak bu durumun da önüne geçebiliriz. Örn;
interface Coder {
	isim: string,
	soyisim: string,
	yas?: number,
};

let codermungan: Coder = {
	isim: "Mehmet Halil",
	soyisim: "MUNGAN",
}

şeklinde yazdığımızda yas keyi versenizde olur vermeseniz de olur. Bu özelliğe Optional Property denmektedir. İsteğe bağlı özellik!

Ufak Çaplı Trickler

  1. İki veya daha fazla type alabilme durumu (Union Types):
type Role = boolean | number; // Boolean yada Number olabilir.

interface User {
	name: string;
	password: string;
	role?: Role; // role Optional Property Boolen veya Number alabilir.
}
  1. Variable atama ve eşleştirme (Literal Types):
type Isim = "Mehmet Halil" | "Mehmet Halil MUNGAN" | "CoderMungan"

interface Coder {
	isim: Isim,
	soyisim: string,
	yas: number,
};

let codermungan : Coder = {
	isim : "Mehmet Halil",
	soyisim : "MUNGAN",
	yas : 30,
};

Generic Types

  • Will soon!

learn-typescript's People

Contributors

codermungan avatar

Watchers

 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.