Typescipt: intersection for setting default props in react - reactjs

I want to use intersection so I dont have to explicitly write every type of my defaultProps but rather take it's type from my declared variable, is that possible? As you can see in my code example even thought myProp is set as nullable, it isn't accepting null value

An intersection combines existing types meaning all the types on both object are present. You cannot use the type of the declared variable to define testProps if its a union of multiple types, as when you set the default value you bind it to a particular type.

Related

What does any[]=[] datatype mean in TypeScript?

I'm working on a project in a company and there's a variable declared like this
chainTypeList: any[]=[];
and I'm not able to access it's values like this.
this.chainTypeList.chainTypeCode;
It shows this error : Property 'chainTypeCode' does not exist on type 'any[]'.
This is looks like typescript's typing.
This means that this is an array of any value. An it's initilized as an empty one.
The any value literally means that you can push any value and value type into this array.
You can't access it by using this:
this.chainTypeList.chainTypeCode;
because it's an array and not an object. That's why you get the error:
Property 'chainTypeCode' does not exist on type 'any[]'.
It should only be something like this:
this.chainTypeList
or
this.chainTypeList[0]
If you want a specific position of the array. Just change the number for the position you want to obtain.
chainTypeList: any[]=[];
It is written as TypeScript not JavaScript. You cannot do this if it is pure JavaScript. It means that variable chainTypeList is declared as an array type any (can be any type), which is initialized as an empty array.
and by calling
this.chainTypeList.chainTypeCode;
you are trying to access property named chainTypeCode in the object chainTypeList. However, the program will not be able to find such property because, as it is declared earlier, the variable is an empty array. It does not have the property you are looking for hence the error
Property 'chainTypeCode' does not exist on type 'any[]'.
What does any[]=[] datatype mean in TypeScript?
It's not a datatype.
The any[] part is a datatype. The = [] part is called an "assignment". An assignment binds a value to a name, e.g. in this case, it binds the value [] (an empty array) to the name chainTypeList, meaning that from this point on, dereferencing the name chainTypeList will evaluate to an empty array.
this.chainTypeList.chainTypeCode;
It shows this error : Property 'chainTypeCode' does not exist on type 'any[]'.
Arrays don't have a property called chainTypeCode, so the error is correct.

eiffel: semantic of ANY default

Surprised that Default in class ANY is frozen and without implementation???, what is the semantic for this function??
Class ANY
frozen default: detachable like Current
-- Default value of object's type
do
end
My intention was to define a default: like Current or maybe detachable which returns the default value for current Class, so redefine it...
This is a default value of a detachable version of a type. For reference types, this is Void. For expanded types, this is the corresponding default value, i.e. the one initialized with default_create. E.g., for BOOLEAN, it is False.
If an expanded class provides a specific implementation of default_create, it is used to initialize Result even without a body in default. For example, consider a class
expanded class X inherit ANY redefine default_create end feature
item: INTEGER_32
default_create do item := 42 end
end
For a variable x of type X, an expression x.default.item would give 42. When default is called, the value of Result is initialized by calling X.default_create that sets item. So, no instructions in the body of default are required.
To summarize, default returns
Void for reference types;
a default value for expanded types that do not redefine default_create, including basic types: False, 0, 0.0, etc. If an expanded type has nested attributes, they are initialized recursively using the same rule.
a value obtained by calling default_create otherwise.

Can React.useMemo second argument array contains an object?

Can React.useMemo second argument array contains an object?
I ask this question because I have an expensive computation based on an object's value.
I don't know should I expand the object, or just simply pass the object into that array.
It is possible to use an object as 2nd argument. But it depends on how the object behaves. If there will be always a new instance of this object each time the affected value(s) has changed, React.useMemo will be able to detect the change. Since React.useMemo will only do an instance compare in case of an object, it will not detect changes within that object if the instance remain the same. If the instance changes more often then the affected properties, it would be better to extract only the required properties and hand them over individually. This will ensure the calculition will only be done if need.

Eiffel: setting a constant with reference to other class constant

How to set a constant refering to another class constant in Eiffel?
Something like that doesn't compile unfortunately
Default_log_level: like {MY_LOGGER}.log_level = {MY_LOGGER}.Log_level_info
Constant attributes cannot be defined using other constant attributes in the current version of Eiffel.
Constant attributes can only be made of a manifest constant, but a possible workaround could be to use frozen once functions:
frozen Default_log_level: INTEGER
once
Result := {MY_LOGGER}.Log_level_info
ensure
definition: Result = {MY_LOGGER}.Log_level_info
end
frozen means that it cannot be redefined in descendant classes (like constant attributes).
Unfortunately, the type of once functions cannot rely on anchored types, hence the use of INTEGER instead of like {MY_LOGGER}.log_level.
And finally, the drawback with this solution is that it cannot be used where constant attributes are expected (e.g. in inspect instructions).

Why would it matter where a type declaration is located?

The documentation for ReasonReact specifies where in the code a type should be declared:
The state type could be anything! An int, a string, a ref or the common record type, which you should declare right before the reducerComponent call:
type state = {counter: int, showPopUp: bool};
let component = ReasonReact.reducerComponent "Dialog";
The emphasis is theirs.
Why would it matter where the type declaration is located, so long as it's valid? Does some kind of optimization take place only when the two lines are adjacent? What happens if I insert other things between them or put them in separate modules?
The type needs to be defined before it is used, but it doesn't matter in any technical sense whether there's anything in between. That's just convention, to keep related things together.
I'll see about getting this clarified in the docs.
Putting the state type (or the retainedProps type or the action type) after the component definition will give you a type error; if you turn on super-errors (like so: https://github.com/reasonml-community/bs-glob/blob/d891ce1fadd3f3b2938d5900eb15241be4a3c1d0/bsconfig.json#L3) then the error briefly explains itself.
In short, it's a corner-case typing issue (scope escape), whose explanations you can find elsewhere.

Resources