How do I compile my TypeScript code for Node.js to one file?

It is not possible to bundle Node.js modules into a single file with the TypeScript compiler alone: each file in the CommonJS module system (used by Node.js) is treated as a single module, and cannot be joined together without proper module bundling techniques found in the many JavaScript code bundlers out there (such as Browserify, … Read more

Typescript : underscore convention for members [closed]

Those who say, must not use the “_”, for them, here is some code from TypeScript site: class Employee { private _fullName: string; get fullName(): string { return this._fullName; } this._fullName = …… } Same question on Stackoverflow, you should have a look on it, especially the answer. For the time being, if accepted, we … Read more

How to exclude getter only properties from type in typescript

While readonly does not directly affect whether types are assignable, it does affect whether they are identical. To test whether two types are identical, we can abuse either (1) the assignability rule for conditional types, which requires that the types after extends be identical, or (2) the inference process for intersection types, which throws out … Read more

What is reason to use is operator in TypeScript?

boolean is just a data type while ‘is’ operator is used for type-testing. Let me change your example a little bit: type Species=”cat” | ‘dog’; interface Pet { species: Species; } class Cat implements Pet { public species: Species=”cat”; public meow(): void { console.log(‘Meow’); } } function petIsCat(pet: Pet): pet is Cat { return pet.species … Read more

TypeScript check for empty string

I believe this is as close as you’re going to get only using the typing system (rather than having a ‘nonEmptyString’ class) type nonEmptyString = never; // Cannot be implicitly cast to function isNonEmptyString(str: string): str is nonEmptyString { return str && str.length > 0; // Or any other logic, removing whitespace, etc. } Testing … Read more

How to extend an interface declared in an external library d.ts?

You can easily extend the ‘knockout’ or any other TypeScript namespace. Example: create knockout-extension.d.ts file /// <reference path=”<path-to-typings-dir>/knockout/index.d.ts” /> declare module ‘knockout’ { export interface CustomType { customField: string; customMethod(arg1: number, arg2: boolean): boolean; } namespace customNamespace { export interface AnotherCustomType { customField1: string; customField2: boolean; } } // NOTE: extending existing interface export interface … Read more

Import module from root path in TypeScript

(Re-posting my answer to avoid puppy-socket.) Using the compilerOptions.baseUrl property I was able to do the below import. This allowed me to have a complete root-to-expected-path, which helps my code maintainance, and works in any file, independently of the current folder. The below examples will have the src folder of my project as the modules … Read more

I want totally immutable object in TS

We now have the option as const which is a syntactically concise way of what @phil294 mentioned as the first option (nested readonly). const a = { b: 33, c: [78, 99], d:{e:{f:{g:true, h:{boom:’selecta’}}}} } as const; a.d.e.f.h.boom = ‘respek’; //Cannot assign to ‘boom’ because it is a read-only property.ts(2540) As an added bonus, you … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)