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:

typescriptlet test1: number = 123;
let test2: string = 'abc';

Bu değişkenleri toplayalım:

typescriptconsole.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:

typescriptlet test1: number = 123;
let test2: string = 'abc';
let test3: number;

test3 = test1 + test2; // string atamaya çalışıyoruz
Задание 1

Kodu çalıştırmadan sonucun ne olacağını belirleyin:

typescriptlet test1: number = 123;
let test2: number = 456;

console.log(test1 + test2);
Задание 2

Kodu çalıştırmadan sonucun ne olacağını belirleyin:

typescriptlet test1: string = '123';
let test2: string = '456';

console.log(test1 + test2);
Задание 3

Kodu çalıştırmadan sonucun ne olacağını belirleyin:

typescriptlet test1: string = '123';
let test2: string = '456';

let test3: number = test1 + test2;
console.log(test3);
Задание 4

Kodu çalıştırmadan sonucun ne olacağını belirleyin:

typescriptlet test1: number = 123;
let test2: number = 456;

let test3: string = test1 + test2;
console.log(test3);
Задание 5

Kodu çalıştırmadan sonucun ne olacağını belirleyin:

typescriptlet test1: number = 123;
let test2: number = 456;

let test3: string = test1 + ' ' + test2;
console.log(test3);
Задание 6

Kodu çalıştırmadan sonucun ne olacağını belirleyin:

typescriptlet test1: number = 123;
let test2: number = 456;
let test3: string = '!';

let test4: string = test1 + test2 + test3;
console.log(test4);
Задание 7

Kodu çalıştırmadan sonucun ne olacağını belirleyin:

typescriptlet test1: number = '123';
let test2: number = '456';

console.log(test1 + test2);
Задание 8

Kodu çalıştırmadan sonucun ne olacağını belirleyin:

typescriptlet test1: number = +'123';
let test2: number = +'456';

console.log(test1 + test2);
Задание 9

Kodu çalıştırmadan sonucun ne olacağını belirleyin:

typescriptlet test: string = '1';
test += 1;

console.log(test);