569 questions
1
vote
2
answers
61
views
How can I define a `BinaryString` type in TypeScript?
I'd like to define a type that will only allow nonempty binary strings. Something like:
type BinChar = "0" | "1"
type BinString = `${BinChar}${BinString}`
const b: BinString = &...
0
votes
0
answers
34
views
How do I invoke my lang key using Handlebars JS inside a Javascript template literal
I'm trying to invoke my lang files (i.e. en.json, es.json, etc.) within a Javascript template literal however, when I call upon the lang file using Handlebars, it renders the lang call as a string ...
0
votes
0
answers
13
views
Template Literal just turns into more string?
I'm learning JavaScript, absolute beginner here. I'm trying to get my first Template Literal to work. For example:
const firstName = "john"
const person1 = `He's &{firstName}`;
What I ...
1
vote
2
answers
64
views
Intersection of Record with generic key extending template literal doesn't infer result of index access
In principle (T & Record<K, U>)[K] should evaluate to U but it doesn't seem to work in the case where K is generic and extends a template literal.
function foo3<
K extends `a${string}`...
-2
votes
3
answers
88
views
How can I write a JavaScript variable inside double quotation marks in a template literal? [closed]
I have this code:
const newDiv = document.createElement('div');
newDiv.innerHTML = `
<button class='btn btn-download'>
<span class='icon'>&#...
2
votes
1
answer
146
views
Using JavaScript template literals `${...}` does not seem to work when using JSP instead of HTML, it returns null in servlet
Im trying to make an AJAX call to a servlet from a login page which is designed as a '.jsp' page.
Im using 'fetch' API for AJAX (code snippet attached below). This script is written in same '.jsp' ...
-1
votes
2
answers
70
views
What is the need of embedding HTML into Javascript code?
I still don’t understand the use of embedding HTML into javascript code. I understand the use of template literals to insert HTML but still not understand why is there a need to embed HTML.
Please ...
0
votes
1
answer
45
views
Escaping, unicode, character escapes while parsing/logging string in javascript
i got a string, which is from user input, i store it in a variable say, str
let str = "some user generated better string that contains unicode escapes \u1451 \u3516 with some otehr escapes \n \t \...
1
vote
1
answer
80
views
in Firefox querySelector with attribute while finding path in svg gives error
My code is working well in Chrome but not in Firefox.
When I search path element using querySelector with attribute Firefox gives an error that path is not found.
defs.appendChild(svgGradient);
const ...
1
vote
3
answers
91
views
html element not being executed
Whenever i am trying to run below line of code, its not executing <br> as break line but actually executing it as string.
const firstFiveLetters = chartData.centerText.slice(0, 5);
const ...
0
votes
1
answer
51
views
How can I construct a variable in JavaScript based on another variables value? And How can I reassign it's value and perform methods on that variable? [duplicate]
I want to be able to basically do this
let x = 1;
let `arr${x}`;
if ((i = 0)) `arr${x}` = [];
`arr${x}`.push(words);
console.log(`arr${x}`);
I have tried using eval()
let x = 1;
eval(`let arr${x}`);
...
1
vote
1
answer
61
views
How to ensure object values are keys of a type deduced by the object key with TypeScript?
Given
// GIVEN
type Animal<T extends string> = {
id: T,
}
type Dog = Animal<"animal.dog"> & {
foo: string
}
type Cat = Animal<"animal.cat"> & {
...
1
vote
1
answer
309
views
TailwindCSS template literals strange behavior
I have the following component:
function SaleBadge({ textContent, badgeColor }) {
return (
<Badge className={`bg-${badgeColor}-200 hover:bg-${badgeColor}-300 animate-pulse align-middle ...
0
votes
2
answers
71
views
Dynamically calling a function based on its name stored in a string
I am trying to dynamically call a function based on its name stored in a string using bracket notation on the window object. I have also tried using eval() to no success.
The project has been bundled ...
1
vote
1
answer
49
views
d3js inline event handler within template literal
In the example below there is an inline event handler that is not working, because there is something wrong with escaping characters. In general inline handlers are bad practice and difficult to ...