Immutable arrays after upgrading angular to 11 - angular11

I just upgraded Angular from 6 to 11 and also typescript to 4.1.5.
I have problems with changing properties of any object from an array. It results with an error
core.js:6150 ERROR TypeError: Cannot assign to read only property 'active' of object '[object Object]'
The object I'm trying to change is:
export interface Agreement {
active: boolean;
...
}
Also when I try to add something to any list, I get this error:
ERROR TypeError: Cannot add property 4, object is not extensible
at Array.push (<anonymous>)
Am I missing any global config property?

I simply created a copy of given field like this:
field = JSON.parse(JSON.stringify(field))

Related

Missing type annotation for `T`. `T` is a type parameter declared in array type [1] and was implicitly instantiated at call of method `slice`

I have a simple js example where I'm getting a weird inferred error.
Missing type annotation for T. T is a type parameter declared in array type [1] and was implicitly instantiated at call of method slice [2].Flow(InferError)
function getChunk(items: Array<Object>, start: number, end: number): Array<Object> {
const chunk = items.slice(start, end)
return chunk
}
I'm unsure why it's trying to infer the array even though I did express it as an array of objects.
Ok so here's the head scratcher.
The error was displayed before I had entered the return type of Array<Object> so the error was a bit weird and didn't express to me that the return type was missing.
But it was not until I relaunched the editor when the return type was accepted. So maybe the flow server was hung or something.

Typescript reflection - required parameters & default values

In short: is there a way to know if a typescript parameter is required and/or has a default value?
Longer version:
Say I have the following file:
//Foo.ts
class Bar {
foo(required:string,defaultValue:number=0,optional?:boolean) {
...
}
}
I would like to know of each of the parameters:
the name
the type
is it required?
does it have a default value?
I have succesfully used method decorators with the TypeScript reflection API to get the types of the parameters, I've used this method to get their names, but so far I have not found a way to know if a variable is required and/or has a default value.
I know the typescript compiler itself can be used from within typescript. So I'm wondering if there is a way to use the parse tree of the compiler to see if a parameter is required and/or has a default value?
How would that work?
If you want to do this from scratch...
On a high level, one way of doing it is to:
Figure out how to get the SourceFile node using the compiler api of your file. That requires a bit of an explanation in itself.
From there, use the api's forEachChild function to loop over all the nodes in the file and find the node with a kind of SyntaxKind.ClassDeclaration and .name property with text Bar.
Then loop over all the children of the class by again using the api's forEachChild function and get the ones that has the kind SyntaxKind.MethodDeclaration and .name property with text foo.
To get the parameters, you will need to loop over the method node's parameters property.
Then for each parameter node, to get the name you can call .getText() on the .name property.
You can tell if the parameter is optional by doing:
const parameterDeclaration = parameterNode as ts.ParameterDeclaration;
const isOptional = parameterDeclaration.questionToken != null || parameterDeclaration.initializer != null || parameterDeclaration.dotDotDotToken != null;
Or you could use the TypeChecker's isOptionalParameter method.
To get its default expression, you will just have to check the initializer property:
propertyDeclaration.initializer;
To get the type use the TypeChecker's getTypeOfSymbolAtLocation method and pass in the symbol of the node... that gets a little bit complicated so I won't bother explaining it (think about how it's different with union types and such).
Don't do it from scratch...
I've created a wrapper around the TypeScript compiler api. Just use this code with ts-simple-ast (edit: Previously this talked about my old ts-type-info library, but ts-simple-ast is much better):
import { Project } from "ts-morph";
// read more about setup here:
// https://ts-morph.com/setup/adding-source-files
const project = new Project({ tsConfigFilePath: "tsconfig.json" });
const sourceFile = project.getSourceFileOrThrow("src/Foo.ts");
const method = sourceFile.getClassOrThrow("Bar").getInstanceMethodOrThrow("foo");
Once you have the method, it's very straightforward to get all the information you need from its parameters:
console.log(method.getName()); // foo
for (const param of method.getParameters()) {
console.log(param.getName());
console.log(param.getType().getText());
console.log(param.isOptional());
console.log(param.getInitializer() != null);
}

null object reference while call other compenet function

I have create the following function in component A(which is productDetailComp)
public function createProductDetailItem(productdetail:Productdetail):void{
productdetailService.createProductdetail(productdetail);
productdetailService.commit();
}
and in the component B, I call the function via
var productdetail:Productdetail = new Productdetail();
productDetailComp.createProductDetailItem(productdetail);
but, why the following error pop up while invoking the createProductDetailItem() function? Need to create a instance for the function or the component productDetailComp??
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at productDetailComp/onEdit()[C:\Users\School\Adobe Flash Builder 4.6\FYPadminSideV3\src\productDetailComp.mxml:80]
at productDetailComp/__button2_click()[C:\Users\School\Adobe Flash Builder 4.6\FYPadminSideV3\src\productDetailComp.mxml:250]

Removing dynamically created component causes error

I have problem with casting custom event currentTarget to component. When I'm trying to removeElement from its parent container i get error. What's the deal?
TypeError: Error #1034: Type Coercion failed: cannot convert mx.events::FlexEvent#8811c41 to com.modultek.pis.itemmachine.search.events.SortEvent.
private function sortSelection_removeHandler(event:SortEvent):void
{
var sortSelection:SortSelection = event.currentTarget as SortSelection;
this.removeElement(sortSelection);
}
Component will be removed but I get this error.
your problem is not in the var sortSelection:SortSelection = event.currentTarget as SortSelection; line at all.
Read the Error message carefully. It tells you that there is an invalid cast from FlexEvent to SortEvent, which happens on
private function sortSelection_removeHandler(event:SortEvent):void
Change event:SortEvent in the function signature to event:FlexEvent or event:Event
Problem was that I didn't have event declaration on my dispatching class.
[Event(name="remove" , type=".....search.events.SortEvent")]

Error During Print

There are a three public variable
public var objPrintJob:FlexPrintJob;
public var objPrintTemplate:canvas;
Fn1:
objPrintJob = new FlexPrintJob();
if (objPrintJob.start() != true)
{
printCount--;
return;
}
Starting the PrintJob;
Fn2:
Am adding the Object and calling send for the Print
objPrintJob.addObject(objPrintTemplate, FlexPrintJobScaleType.SHOW_ALL);
objPrintJob.send();
During Run
It Throws the Error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mx.printing::FlexPrintJob/addObject()[E:\dev\4.0.0\frameworks\projects\framework\src\mx\printing\FlexPrintJob.as:252]
at org.dckap.mafcote.views.mediators::WorkAreaMediator/parseXML()[F:\MafcoteMGP\MafcoteMGP\src\org\dckap\mafcote\views\mediators\WorkAreaMediator.as:3243]
at org.dckap.mafcote.views.mediators::WorkAreaMediator/onSaveTemplateWithCredits()[F:\MafcoteMGP\MafcoteMGP\src\org\dckap\mafcote\views\mediators\WorkAreaMediator.as:2896]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\4.0.0\frameworks\projects\framework\src\mx\core\UIComponent.as:12266]
at org.dckap.mafcote.views::CreditsShowPanel/okButtonClickHandler()[F:\MafcoteMGP\MafcoteMGP\src\org\dckap\mafcote\views\CreditsShowPanel.mxml:41]
at org.dckap.mafcote.views::CreditsShowPanel/__btnOk_click()[F:\MafcoteMGP\MafcoteMGP\src\org\dckap\mafcote\views\CreditsShowPanel.mxml:159]
Need Help For This..
}
This has to be that your objPrintTemplate object is null. The stack trace in your error shows that it's erroring in FlexPrintJob/addObject(), which means it made it into that method but did not complete executing the method. Since the only parameters you're sending to it are your object reference and a constant value, the object must be null.
After your support I found out my solution.
Thank U all.
The object does not seems to be null.
I did one mistake that is i didn't added the object as a child anywhere else.
so the addObject() in print requires the systemManager.
which will be null if i didn't add as a child.
Thank You for your support

Resources