TypeScript'te Veri İşlemleri
TypeScript, katı tiplemeye sahip diğer dillerin aksine, farklı veri türleri üzerinde işlemler yapmaya, onları ortak bir türe dönüştürmeden izin verir.
Bir örnekle ne demek istediğimize bakalım. Bir string ve bir numeric değişkenimiz olduğunu varsayalım:
let test1: number = 123;
let test2: string = 'abc';
Bu değişkenleri toplayalım:
console.log(test1 + test2); // '123abc' yazacak
Yani, örneğin string ve number'lar toplanabilir ve bu bir hataya yol açmaz. Yani TypeScript sadece programcının değişkenin veri türünü değiştirmediğinden emin olur.
Örnek olarak, aşağıdaki kodda bir numeric değişkene string atamaya çalışıyoruz ve bu bir hataya neden olacak:
let test1: number = 123;
let test2: string = 'abc';
let test3: number;
test3 = test1 + test2; // string atamaya çalışıyoruz
Kodu çalıştırmadan sonucun ne olacağını belirleyin:
let test1: number = 123;
let test2: number = 456;
console.log(test1 + test2);
Kodu çalıştırmadan sonucun ne olacağını belirleyin:
let test1: string = '123';
let test2: string = '456';
console.log(test1 + test2);
Kodu çalıştırmadan sonucun ne olacağını belirleyin:
let test1: string = '123';
let test2: string = '456';
let test3: number = test1 + test2;
console.log(test3);
Kodu çalıştırmadan sonucun ne olacağını belirleyin:
let test1: number = 123;
let test2: number = 456;
let test3: string = test1 + test2;
console.log(test3);
Kodu çalıştırmadan sonucun ne olacağını belirleyin:
let test1: number = 123;
let test2: number = 456;
let test3: string = test1 + ' ' + test2;
console.log(test3);
Kodu çalıştırmadan sonucun ne olacağını belirleyin:
let test1: number = 123;
let test2: number = 456;
let test3: string = '!';
let test4: string = test1 + test2 + test3;
console.log(test4);
Kodu çalıştırmadan sonucun ne olacağını belirleyin:
let test1: number = '123';
let test2: number = '456';
console.log(test1 + test2);
Kodu çalıştırmadan sonucun ne olacağını belirleyin:
let test1: number = +'123';
let test2: number = +'456';
console.log(test1 + test2);
Kodu çalıştırmadan sonucun ne olacağını belirleyin:
let test: string = '1';
test += 1;
console.log(test);