I have array with variable names:
var subjectArray:Array=["subject0","subject1","subject2"];
I need to convert string to var, but following does not work: this[subjectArray[0]] throws an error.
Any thoughts?
That syntax should work. You can check if an object contains a property with a given name with the in keyword. The property does probably not exist.
if (subjectArray[0] in this) {
// do something with this[subjectArray[0]];
}
Related
TypeScript does not produce any errors for the following code:
const maybe_a_string: undefined | string = undefined;
const false_or_string: false | string = false;
// I'd like the following to produce an error/warning...
const message_string = `Some readable string info should be here: ${maybe_a_string} ${false_or_string}`;
Is there some kind of setting I can turn on, or simple alternative ways to write the last line that will warn me about trying to use non-string variables inside strings like this? (but without needing to add extra lines of code for every sub-string to be asserted individually)
I guess it treats them as fine because some types like bools, numbers and misc objects have a .toString() method...
But especially in the case of undefined (which actually doesn't have a .toString() method) - it's quite common for you to have a bug there, as the only time you really want to see the string "undefined" inside another string is for debugging purposes. But there's a lot of these bugs out there in the wild where end users are seeing stuff like "hello undefined" unintentionally.
Personally I would handle this by making the string template into a function. That way you can specify that the arguments must be strings.
const createMessageString = (first: string, second: string): string => {
return `Some readable string info should be here: ${first} ${second}`;
}
const message_string = createMessageString( maybe_a_string, false_or_string );
// will give an error unless types are refined
Vote for https://github.com/microsoft/TypeScript/issues/30239 [Restrict template literal interpolation expressions to strings]
Additionally, you can try workarounds from the issue comments.
I'm trying to compare two strings in Jenkins pipeline. The code more or less look like this:
script {
def str1 = 'test1.domainname-test.com'
def str2 = 'test1.domainname-test.com'
if ( str1 == str2 ) {
currentBuild.result = 'ABORT'
error("TENANT_NAME $TENANT_NAME.domainname-test.com is already defined in domainname-test.com record set. Please specify unique name. Exiting...")
}
}
str1 is fed by a preceeding command I skipped here due to simplicity. I am getting this error:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field java.lang.String domainname
What am I doing wrong? I tried equals method too, same result. As if it stucked on those dots, thinking it's some kind of property. Thanks in advance
You're missing curly brackets surrounding the TENANT_NAME variable name. In your example:
error("TENANT_NAME $TENANT_NAME.domainname-test.com is already defined in domainname-test.com record set. Please specify unique name. Exiting...")
the $ sign gets applied to TENANT_NAME.domainname. And because TENANT_NAME is a string, Groovy interprets the following part as you were trying to access domainname property from a String class, and you get No such field found: field java.lang.String domainname exception.
To avoid such problems, wrap your variable name with {} and you will be fine.
error("TENANT_NAME ${TENANT_NAME}.domainname-test.com is already defined in domainname-test.com record set. Please specify unique name. Exiting...")
I am trying to define GraphQL schema like this:
type Obj {
id: Int
0_100: Int
}
But it gives following exception.
'GraphQLError: Syntax Error: Expected Name, found Int "0"',
How can I define attribute starting with numeric, -, + signs.
This is the regexp for names in GraphQL: /[_A-Za-z][_0-9A-Za-z]*/. Anything that does not match is not allowed.
Sample URL:
http://facebook.github.io/graphql/June2018/#sec-Names
Numerical parameter names do not work in GraphQL.
You can probably prefix it with a string like _0_100, but it's fairly unusual and I'd recommend against it. Consider using words to name your parameters instead.
I'm trying to use a string variable as input to an xml function. When I use this command:
name2_node(i).setTextContent('truck');
there is no error. But when I replace it with:
name2_node(i).setTextContent(type(i,1));
an error occurs like this:
No method 'setTextContent' with matching signature found for class
'org.apache.xerces.dom.ElementImpl'.
The variable type is a string array. In fact when I type type(i,1) in command window the result is:
ans =
string
"truck"
What part am I doing wrong?
Two things:
use a different variable name, type is a built in function which tells you the variable type, hence why it shows "string" in the output.
Then access the cell array of strings with curly braces
vehicletypes = {'car'; 'truck'; 'van'};
name2_node(i).setTextContent(vehicletypes{i,1}); % For i=2, this passes 'truck'
I have a velocity variable, like this:
$cur_record.getFieldValue("SelectRoles", $locale)
that is supposed to be an array. If I print its value, (just by putting $cur_record.getFieldValue("SelectRoles", $locale) in the code) i get:
["Accountant","Cashier"]
now, i want to iterate those 2 values, Accountant and Cashier, but it seems to be a String, not an Array, how can i convert that to an array so I can iterate it?..
I have tried to iterate it, but does not work, like this:
#foreach($bla_role in $cur_record.getFieldValue("SelectRoles", $locale))
$bla_role
#end
Also tried to get the value, as if it were an array, does not work either:
$cur_record.getFieldValue("SelectRoles", $locale).get(0)
I've tried setting it to another variable, like this:
#set($roleval = $cur_record.getFieldValue("SelectRoles", $locale))
$roleval.get(0)
but it does not work, but if i set a string, as the value is printed (the value hard coded), it does work!, like this:
#set($roleval = ["Accountant","Cashier"])
$roleval.get(0)
I dont know if I have to escape something, or I am missing something, can some one help me?
thank you!
You're trying to parse a String array that was previously serialized to String.
The following snippet uses substring, split and replace String methods to parse it.
#set($roleval = '["Accountant","Cashier"]')
#set($rolevalLengthMinusOne = $roleval.length() - 1)
#set($roles = $roleval.substring(1, $rolevalLengthMinusOne).split(","))
#foreach($role in $roles)
<h1>$role.replace('"',"")</h1>
#end
At first I tried to use #evaluate to parse it, but I ended up with these String methods.