logo
Tags down

shadow

Typescript dynamically visible class methods


By : Jill Chih
Date : September 17 2020, 03:00 AM
this one helps. First: visibility is only checked during compile time, so bear in mind that there's no way to throw an error at runtime if you want to block access to a method. If this is your use-case, try something else.
If you want compile-time checking, what you're properly really after is better types.
code :
class Foo {
   method1() {}
   method2() {}
}
type FooMethod2Only {
   method2: () => void;
}
function doSomething(foo: FooMethod2Only) {
  foo.method1();
}
doSomething(new Foo());


Share : facebook icon twitter icon

Dynamically loading a typescript class (reflection for typescript)


By : Chetna Wadhwa
Date : March 29 2020, 07:55 AM
Any of those help I would like to be able to instantiate a typescript class where I get the class and constructor details at runtime. The function I would like to write will take in the class name and constructor parameters. , You could try:
code :
var newInstance = Object.create(window[className].prototype);
newInstance.constructor.apply(newInstance, instanceparameters);
return newInstance;
class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}

//instance creation here
var greeter = Object.create(window["Greeter"].prototype);
greeter.constructor.apply(greeter, new Array("World"));

var button = document.createElement('button');
button.innerText = "Say Hello";
button.onclick = function() {
    alert(greeter.greet());
}

document.body.appendChild(button);

Dynamically access methods of class TypeScript


By : رميس فروخ
Date : March 29 2020, 07:55 AM
To fix this issue We simply have to leave strongly typed (and checked) world, and use just a JavaScript style (which is still useful, e.g. in these cases)
code :
fooBar[methodName]();

How do I dynamically export Class methods as standalone functions in TypeScript?


By : wl2016
Date : March 29 2020, 07:55 AM
To fix the issue you can do I am rewriting an old NPM module in TypeScript, and I ran into an interesting problem. , Yes, we can! © In "Thing" file combine default export and export:
code :
export class Thing {
    init() { }
}

export default new Thing();

typescript class decorator:typescript compiler doesn't recognize abstract methods


By : Andrew
Date : March 29 2020, 07:55 AM
this one helps. The problem is that a class decorator is applied to the constructor of the class, not to an instance of the class.
That is, given a constructor type Ctor like
code :
type Ctor<T> = {
    new(...args: any[]): T;
    prototype: T
}
function ValidationDecorator() {
  return function<T extends Ctor<BaseValidation>>(target: T) {
    // impl
  }
}
const validationMap: { [k: string]: Ctor<BaseValidation> | undefined } = {};
function ValidationDecorator() {
  return function<T extends Ctor<BaseValidation>>(target: T) {
    // assuming that getValidationName is a method defined on the prototype
    validationMap[target.prototype.getValidationName()] = target;
  };
}
@ValidationDecorator()
class SomeValidation extends BaseValidation {
  public getValidationName(): string {
    return "ValidationName";
  }

  public validation(): boolean {
    return false;
  }
}

@ValidationDecorator()
class SomeOtherValidation extends BaseValidation {
  public getValidationName(): string {
    return "OtherValidationName";
  }
  public validation(): boolean {
    return false;
  }
}
Object.keys(validationMap).forEach(k =>
  console.log(k + ": " + (validationMap[k] || { name: "oops" }).name)
);
// ValidationName: SomeValidation
// OtherValidationName: SomeOtherValidation
const simplerValidationMap = {
  ValidationName: (thingToValidate: any) => false,
  OtherValidationName: (thingToValidate: any) => false
}

Adding methods dynamically to a class in typescript


By : user3313352
Date : March 29 2020, 07:55 AM
will be helpful for those in need You should be using
extclass.prototype['modifyLogger'] = function(mylogger:any){ }
code :
interface extclass{
    modifyLogger(arg: any): void;
}
class Extclass { 
    test() {

    }
}
interface Extclass {
    modifyLogger(): void;
}

class Test {
    constructor() { 
        Extclass.prototype['modifyLogger'] = function () {
            alert('hi');
        }
        var obj = new Extclass();
        obj.modifyLogger();
    }
}

var t = new Test();
Related Posts Related Posts :
  • TypeScript Compiler API function which can check if a class implements an interface
  • typescript currying call results and function definition
  • How would I enforce at-least one in many optional properties for a TypeScript interface?
  • Parameters generic of overloaded function doesn't contain all options
  • What is the benefit of union types in Typescript
  • Type inference with generic array in Typescript
  • Why TS is unsafe in unary arithmetic operations but not in binary ones?
  • Why does TS let me return wrong type in overloaded function declarations?
  • How do I tell TypeScript that interface T is narrower than type U which has an index signature?
  • Why doesn't TypeScript type guard 'in' narrows types to keyof types?
  • Why we need @types in TypeScript
  • a number between 0 and 1 as a type in typescript
  • Rxjs Transform API result Observable to another Observable in a service function
  • Generic pattern-matching for tagged unions
  • NullInjectorError: StaticInjectorError(BootModule)[BsDropdownDirective -> AnimationBuilder]: NullInjectorError: No pr
  • Observable nested callbacks
  • Conditionally casting the input to a Typescript function
  • How do I connect express to vue app with axios
  • Simple import statements for npm package
  • React Native with typescript unable to resolve modules
  • Conditional response type
  • Typescript abstract property
  • typescript instanceof return false
  • How can I type a function that takes an arbitrary number of input functions such that its return type can be determined?
  • Acessing type as object or Array in Typescript
  • Operator '+' cannot be applied to types 'T' and 'T'. in TypeScript
  • Type is changing in TypeScript
  • Discriminate union types
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk