ืืืึธืก ืืขื ืขื ืืืกืืขืจ ืืืืขืจืึทื ืืืืคึผืก
ืืื ื ืืืคึผืขืกืงืจืืคึผื 4.1, ืืืจ ืงืขื ืขื ืคึฟืึทืจืืขืกืขืจื ืื ืืืืคึผืื ื ืคึฟืึทืจ ืืืื ืกืืจืื ืืก ืืืืืืืืง. ื ืึธืจ ืฆื ืืขืฉืืืื ื ืจืืงืึทืคึผื, ืืื ืืืคึผืขืกืงืจืืคึผื ืืืจ ืงืขื ืขื ืืขืคืื ืืจื ืึทืืื ืืขืจืืคืขื ืข "ืืืืขืจืึทื ืืืืคึผืก". ืึทื ืฉืืึธื ืคึผืื ืงื ืืืคืืื ืื ื ืึท ืืืึทืืขืืืืืง ืืื ืฉืืจืืงื, ืืืจ ืงืขื ื ืืึทืืจืขื ืขืฆื ืืขื ืืืคึผ ืฆื ืึท ืกืคึผืขืฆืืคืืฉ ืกืืื ืคืื ืกืืจืื ืืก ืืืึธืก ืืขื ืขื ืขืจืืืืื.
// Define a mutable string as 'Literal Type'.
let key: "name" | "department" = "name";
// This works, as 'department' is part of the type.
key = "department";
// Won't work, as it's not define in the union type.
key = "company";
// Of course, the same applies for functions.
function getKey(): "name" | "department" {
return Math.random() <= 0.5 ? "name" : "department";
}
ืกืืึทืจืืื ื ืืื ืืืคึผืขืกืงืจืืคึผื 4.1, ืืขื ืืึทืืจืืฃ ืืื ืืืืืืขืจ ืืขืืืขืืึธืคึผืขื. ืืืจื ื ืขืืขื "ืืืกืืขืจ ืืืืขืจืึทื ืกืืจืื ืืก" ืคึฟืื Javascript ืืื ืื ืกืคึผืืจืึทืฆืืข, ืืืจ ืงืขื ืขื ืฆืืืืืื ืื ืืขืืืข ืกืื ืืึทืงืก ืคึฟืึทืจ ืืืื ืืืืขืจืึทื ืืืืคึผืก ืืื ืืืืคึผืกืงืจืืคึผื. ืืขืจ ืืืืคึผื ืืืืืง ืืื ืึทื ืืื "ืืขืืคึผืืึทืืข ืืืืขืจืึทื ืืืืคึผืก" ืืืจ ืืึทืงืืืขื ืคืื ืืขืจ ืืืืืืงืืื, ืืืืึทื ืื ืืืคึผ ืืื ืงืขื ืขื ื ืขืืขื ืคึผืึทืจืึทืืขืืขืจืก ืคึฟืึทืจ ืืขื ืคืึธืจืขื - ืืขืจ ืืึทืืจืืฃ ืืื ืขื ืืขื ืฆื ืืืฉืึทื ืขืจืืงืก, ืึธืืขืจ ืึท ืืืกื ืึทื ืืขืจืฉ ืืืืึทื ืขืก ืจืืงืืืืืขืจื ืกืคึผืขืฆืืคืืฉ ืืืึทืืืขืก.
// Define a Literal Type
type State = "ready";
// Nice, isn't it? As in Javascript's
// 'template literal strings', we can
// reuse the same syntax for TS.
//
// The create type here is now 'Key-ready'.
type Key = `key-${State}`;
// This of course works for union types as well.
// Let's take these non-descriptive types for a
// quick demo:
type GraphState = "sleep" | "ready" | "error";
type MachineState = "booting" | "running";
// And here's the 'template literal type':
type EntityState = `ES-${GraphState | MachineState}`;
// The possible combinations therefore
// work out like this:
//
// type EntityState = 'ES-sleep'
// | 'ES-ready'
// | 'ES-error'
// | 'ES-booting'
// | 'ES-running'
ืขืก ืก ืืคืืื ืืขืจ ืืื ืืืืคึผืกืงืจืืคึผื 4.3
ืืฉืขืช ืืื ืฉืจืืื ืืขื, ืืขืจ ืืขืืืื ื ืงืึทื ืืืืึทื ืคึฟืึทืจ ืืืคึผืขืกืงืจืืคึผื 4.3 ืืื ืืืคืจืืื, ืืืึธืก ืืืื ืืืืึทืืขืจ ืขื ืืึทื ืกืืึทื ืฅ ืฆื ืืืกืืขืจ ืืืืขืจืึทื ืืืืคึผืก.
ืืขืจ ืืืคึผ ืกืืกืืขื ืืื ืืืฆื ืืคืืื ืืขืจ ืืื ืึทืืืฉ ืืื ืงืขื ืขื ืืขืกืขืจ ืืืกืคืืจื ืื ืจืืืืืง ืืืืคึผืก ืคึฟืึทืจ ืืืจ. ืื ืืขืฆืืข ืขื ืืขืจืื ืืขื ืืขื ืขื ืืขืจ ืืขืจืืืึทืงืกื ืืืกืืขืจ ืฉืืจืืงื ืืืคึผ.
// This example is taken 1:1 from
// Typescript's 4.3 announcement.
// Please check out the link in the
// addendum to see all examples!
declare let s: string;
declare function f<T extends string>(x: T): T;
// Previously: string
// Now : `hello-${string}`
let x2 = f(`hello ${s}`);
ืืกืงื ื
ืืื ืืืจ ืืึธื ืืขืืขื, ืืืคึผืขืกืงืจืืคึผื ืืื ืึทืืึท ืึท ืฉืืึทืจืง ืฉืคึผืจืึทื ืืืขื ืขืก ืงืืื ืฆื ืืืคืืื ืื ื ืืื ืึทืืืฉ ืืืืคึผืก. ืืึธืก ืืื ืกืคึผืขืฆืืขื ืืืช ืคึฟืึทืจ ืืืืคึผืก-ืืืืืจื ืืืืคึผืก. ืืื ืื ืืขืฆืืข ืขื ืืขืจืื ืืขื ืืื ืืืขืจืกืืข 4.3, ืื ืืืคึผ ืกืืกืืขื ืงืขื ืขื ืืขืกืขืจ ืืืกืคืืจื ืื ืจืืืืืง ืืืืคึผืก ืืื ืืืฅ, ืืืึธืก ืืืื ืืจืืขืื ืืื ืืืืคึผืกืงืจืืคึผื ื ืึธืจ ืึท ืคืึทืจืืขื ืืื.