In angular I have two files
in the first file
private _test: BehaviorSubject<any> = new BehaviorSubject({});
In the second file
test$: Observable<Object> = this.test2;
when I change in first file .ts variable from _test to test I have an error
Property 'test' is private and only accessible within the class
In some article:
A convention has also developed regarding the use of _, which is frequently used to preface the name of an object's property or method that is private
Why using underscore sign project compiles without errors?
In typescript / Javascript ES6, getter are defined that way:
get test() : any {
return this._test;
}
because of that, you can't have both a property and a function with the same name.
This is also why, when generating getter/setter, your IDE will usually renamed the property with underscore (_).
You could rename the property to remove the underscore, changing it's visibility to public, and remove the function. in both case Your property will be accessible like that obj.test.
Related
When I add a function to the Math object, it does not show inside IntelliSense of VSCode.
Math.dot = function (a,b) {};
If you don't have one already, create a type declaration module file (Ex. index.d.ts) in the root of your project folder, and add to it the following:
declare interface Math {
doc: (a:number, b:number) => number;
}
You can read more about type declaration module files in the official docs at https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html.
In simpler cases, you can just use JS Doc comments, since a basic subset of JS Doc comments are supported by VS Code's intellisense facilities. As far as I know, this is not one of those supported cases (declaring the type of a function on an existing global object).
On this page, It says that to make properties limited to being read-only, use the get keyword. However I do not see the point of adding the get lang() function that basically returns the language property. I mean, yes you'd not be able to do something like person.lang = 'whatever', but you would be able to do person.language = 'whatever'. How would I properly restrict that access using getters, not writable: false?
If you want to make a field private use private class fields
class Foo {
#language
constructor(lang) {
this.#language = lang;
}
get lang() {
return this.#language;
}
}
const f = new Foo('Japanese');
console.log(f.lang);
//f.#language = 'German'; // error!
note: private class fields are relatively new. you'll need to use a transpiler like babel to support old browsers.
They aren't necessarily for restricting access, though they can be, but they actually run code while accessing the variable, kinda like a syntactic equivalent of .lang() (which I'd call a thunk)
You can prevent anyone from tampering with the variable by restricting them to 'read-only' and disabling that writable property in defineProperty.
If you remove public access to the underlying property, the instance can act as an accessor or proxy to it (the class instance could have a reference to a private scope that it was generated in, for example). In that manner, you'd never be able to write a value to the accessor's field, but you could still access the otherwise privately scoped variable.
If not a factory pattern like that, I could imagine defining the getter function at a later time via some third party which holds private variables. You could also pass a third party:
the instance
the data store
some sort of registry that can verify the instance is authenticated
This third party could connect it's getter to the otherwise private data store variable, rather than during instantiation as in a factory pattern
I believe privatization comes with additional layers, and the get/set thing is just syntactic
I've seen a number of issues in the web project I'm working on where someone uses the "this" object accidentally. This happens most often when someone does a copy-paste from a method inside a class into a pure function. This almost always results in a runtime error "Cannot access XXXXXX in undefined" or something similar in the JS console.
Is there any easy way to have Typescript generate an error in this case? I haven't found a compiler option that does it. Do I have to install something like TSLint?
I haven't found a compiler option that does it.
The compiler option you are looking for is noImplicitThis š¹
More
From : https://www.typescriptlang.org/docs/handbook/compiler-options.html
Raise error on this expressions with an implied any type.
The noImplicitThis flag recommended by Basarat is the first step.
But what should you do when that flag causes TypeScript to error? If you insist on having your method as a standalone function, you can maintain type safety by defining this explicitly. Consider this example:
class Person {
constructor(readonly name: string) {}
greet() {
console.log(`Hello, my name is ${this.name}`);
}
}
const person = new Person('Bob');
person.greet();
This works great. But what if you want greet to be a function on its own?
The solution: tell TypeScript that greet can only be called with this of Person.
function greet(this: Person) {
console.log(`Hello, my name is ${this.name}`);
}
greet.call(person)
The this parameter always comes first on the parameters list.
I'm trying to use Idris with Nativescript, by using the Idris JavaScript FFI and using JavaScript as the codegen target. However, it seems like Nativescript makes use of global variables, such as an object called global. How would I be able to work with that object from within Idris?
You can write separate getter and setter functions using FFI (both in JS_IO of course), and then you can start coming up with whatever abstractions you want to build on top of it:
getVar : JS_IO String
getVar = foreign FFI_JS "globalVar" (JS_IO String)
setVar : String -> JS_IO ()
setVar = foreign FFI_JS "globalVar = %0" (String -> JS_IO ())
Someone on a mailing list suggested this, and it worked.
It's just, using do notation:
global <- foreign FFI_JS "global" (JS_IO Ptr)
Is it in someway possible to reuse a class name that is defined in the Global objects list found here? (Like Number, String)
Lets say that I want to have my own String class. I can define it, and use it like this:
String.js
export default class String {
}
App.js
import String from './String'
let string = new String();
This actually works, but then
PHPStorm tells me: yeah you used a primitive object wrapper (thinking it's
the global object String).
Then ESLint tells me: Disallow Primitive Wrapper Instances (no-new-wrappers)
And lastly SonarQube tells me: The use of wrapper objects for primitive types is gratuitous, confusing and dangerous. Simple literals should be used instead.
So yeah is there a way to encapsulate my class so it doesn't get confused with the global String class?
import './String'
Function and class definitions are local to the module. You're importing the file, but not using anything from it.
That's why this:
let string = new String();
will use the global Stringāthere is no String definition in the module scope.
Make sure you name the imports you need:
import String from './String'
Incidentally, this is one of multiple reasons why it is better practice to give your String class a unique name, like PhysicsString or StringMaterial or Thread. That way if you forget to import it somewhere, you won't accidentally be using the global definition.