-
-
Notifications
You must be signed in to change notification settings - Fork 30
What's new
Alex D edited this page Aug 4, 2023
·
4 revisions
function main()
{
let a: 10 = 11; // error
let b = 11; // ok
b = 10; // ok
print("done.");
}
tsc --opt --emit=exe <file>.ts
Export for functions (from previous example)
import './shared_lib'
function main()
{
test1();
}
shared_lib.ts
- shared library file:
export function test1()
{
print("Hello World!");
}
- Load shared library
function LoadFunction(dllName: string, funcName: string)
{
LoadLibraryPermanently(dllName);
return SearchForAddressOfSymbol(funcName);
}
let test1: () => void = LoadFunction("./shared_lib.dll", "test1");
function main()
{
test1();
}
tsc --opt_level=0 --di --emit=obj <file>.ts
function foo1(f?: (a: number) => number) {
f ??= ((a: number) => a)
f(42);
}
function foo2(f?: (a: number) => number) {
f ||= ((a: number) => a)
f(42);
}
function foo3(f?: (a: number) => number) {
f &&= ((a: number) => a)
f(42);
}
function main() {
foo1();
foo2();
foo3((a: number) => a * 2);
}
function main() {
type StaffAccount = [number, string, string, string?];
const staff: StaffAccount[] = [
[0, "Adankwo", "adankwo.e@"],
[1, "Kanokwan", "kanokwan.s@"],
[2, "Aneurin", "aneurin.s@", "Supervisor"],
];
for (const v of staff) print(v[0], v[1], v[2], v[3] || "<no value>");
assert(staff[2][3] == "Supervisor");
print("done.");
}
function* g() {
yield* (function* () {
yield 1.0;
yield 2.0;
yield "3.0";
yield 4.0;
})();
}
function main() {
for (const x of g())
if (typeof x == "string")
print("string: ", x);
else if (typeof x == "number")
print("number: ", x);
}
toPrimitive
const object1 = {
[Symbol.toPrimitive](hint: string) : string | number | Boolean {
if (hint === "number") {
return 10;
}
if (hint === "string") {
return "hello";
}
return true;
}
};
print(+object1); // 10 hint is "number"
print(`${object1}`); // "hello" hint is "string"
print(object1 + ""); // "true" hint is "default"
interator
class StringIterator {
next() {
return {
done: true,
value: ""
};
}
[Symbol.iterator]() {
return this;
}
}
function main() {
for (const v of new StringIterator) { }
}
class Lib {
static max<T>(a: T, b: T): T {
return a > b ? a : b;
}
}
function main() {
assert(Lib.max(10, 20) == 20);
assert(Lib.max("a", "b") == "b");
assert(Lib.max<number>(20, 30) == 30);
print("done.");
}
TypeScript Native Compiler Wiki