Functions
Π€ΡΠ½ΠΊΡΠΈΠΈ
function add(a: number, b: number): number {
return a + b
}
function test(_:number): void {}
// _ β ΡΡΠΎ ΡΠΊΠ°Π·ΡΠ²Π°Π΅Ρ ΠΊΠΎΠΌΠΏΠΈΠ»ΡΡΠΎΡΡ, ΡΡΠΎ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ ΠΌΠΎΠΆΠ΅Ρ Π½Π΅ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡΡΡ ΠΈ ΠΌΡ ΡΡΠΎ Π·Π½Π°Π΅ΠΌ
function test(): number & string {} // ΡΡΠΎ ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ Π²Π΅ΡΠ½Π΅ΡΡΡ ΠΎΠ±ΡΠ΅ΠΊΡ, Ρ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΠΊΠ»ΡΡΠΈ Π±ΡΠ΄ΡΠ΄Ρ ΠΈΠΌΠ΅ΡΡ ΡΠΈΠΏ number ΠΈ ΡΠΈΠΏ string
// ΠΠΏΠΈΡΠ°Π½ΠΈΠ΅ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌΠΎΠ³ΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΡ:
interface ILength {
length: number
}
function withCount<T extends ILength>(value: T): {value: T, count: string} {
return {
value,
count: `Count: ${value.length}`
}
}
ΠΠ΅ΡΠ΅Π³ΡΡΠ·ΠΊΠ° ΡΡΠ½ΠΊΡΠΈΠΉ
interface MyPosition {
x: number | undefined
y: number | undefined
}
interface MyPositionWithDefault extends MyPosition {
default: string
}
// ΠΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅ΠΌ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΡΠ΅ ΡΠΏΠΎΡΠΎΠ±Ρ Π²ΡΠ·ΠΎΠ²Π°
function position(): MyPosition
function position(a: number): MyPositionWithDefault
function position(a: number, b: number): MyPosition
// ΠΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅ΠΌ ΡΠ°ΠΌΡ ΡΡΠ½ΠΊΡΠΈΡ
function position(a?: number, b?: number) {
if (!a && !b) {
return {x: undefined, y: undefined}
}
if (a && !b) {
return {x: a, y: undefined, default: a.toString()}
}
return {x: a, y: b}
}
Lambda
const split = (a: string, s: string): string[] => a.split(s)
Last updated