Questions tagged [typescript]

33783 questions
1

votes
1

answer
3.5k

Views

Webpack: You may need an appropriate loader to handle this file

I am setting up typescript with webpack, using awesome-typescript-loader. However webpack is giving me this error on build: ERROR in ./src/logic/Something.ts Module parse failed: Unexpected token (2:19) You may need an appropriate loader to handle this file type. here is a piece of code from webpac...
Martin Shishkov
1

votes
1

answer
307

Views

Use string variables in typescript's enum

Is it possible in typescript to use string variables in enum? I can use strings in enum like this: enum AllDirections { TOP = 'top', BOTTOM = 'bottom', LEFT = 'left', RIGHT = 'right', } But this code: const top: string = 'top' const bottom: string = 'bottom' const left: string = 'left' const right:...
Anton
1

votes
2

answer
3.3k

Views

Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED

I have been developing an angular application with restful API get(). I have got few errors like unauthorization error:401 which has been solved. Now I am getting this error which is kind of straight-forward. I have tried added CORS package as well. I have been searching for solutions which are in v...
cantona_7
1

votes
1

answer
927

Views

React router in TypeScript- both router and own props

my component has some own props class Props { wizard: WizardConfig; } and I want to access router history so I also pass router props type PropsType = Props & RouteComponentProps; class Wizard extends React.Component {} The problem is that in component's usage I get errors that RouteComponentProps p...
micnyk
1

votes
1

answer
624

Views

How can I unit test a custom element

I have a class that extends HTMLElement. From reading this thread https://github.com/Microsoft/TypeScript/issues/574#issuecomment-231683089 I see that I can't instantiate a custom element with out getting a Illegal constructor. Additionally, the registerElement looked promising as it returned the...
Jonathan Fishbein
2

votes
0

answer
20

Views

to change the tab index of mat-stepper using output

i am using mat-stepper for my view and in each mat-step there is a component with form. when my form gets submitted i am passing an event using @output and receiving in parent component and incrementing the index of mat-stepper using stepper.next() but it is now incrementing for the first time it is...
subhashish negi
1

votes
1

answer
455

Views

TypeScript: How does typeof work when called on a class?

As far as I understand it, the TypeScript 'typeof' operator performs a type query, and allows you to ensure that two objects are the same type even when that type is unnamed. However, I've found some code in production that does something like this: function println (x) { document.write(x + ''); } c...
nupanick
1

votes
1

answer
2.8k

Views

Angular - Access values of object stored in array of objects inside template

I've got a single class as below: export class SettingsTab { id: number; class: string; name: Object = {}; } Also I mock it into simple array: export const SETTINGSTABS: SettingsTab[] = [ { id: 1, class: 'classOne', name: [{ 'pl': 'Tekst po polsku' }, { 'en': 'Text in english' }] }, { id: 2, class:...
Ashiv3r
1

votes
2

answer
225

Views

How to use Typescript as compiler in node-migrate

To start, here's my migration script: const bluebird = require('bluebird'); const mongoose = require('mongoose'); mongoose.Promise = bluebird; mongoose.connect('mongodb://localhost:27017/app', { useMongoClient: true }); const schema = { uid: { type: String, }, parentID: { type: String...
Christopher Santos
1

votes
1

answer
896

Views

Argument of type 'string[]' is not assignable to parameter of type 'ReadonlyArray<number>'

I am trying to learn d3.js from this tutorial I draw figures using d3.js in angular but I am getting stuck here. can anyone please tell me how can I solve this error. I am having only one method drawSvg() and I am getting an error which is Argument of type 'string[]' is not assignable to parameter...
Humble Dolt
1

votes
1

answer
2.4k

Views

How can i get selected value from a select component

How can I get the selected value from a select component? select.component.ts: export class PfSelectComponent implements OnInit { constructor() { } ngOnInit() { } @Input() options : Array; } select.component.html {{option.name}} select value: {{selectedValue}} {{selectValue}} does not show the value...
Rafael Augusto
1

votes
1

answer
79

Views

Contiguous allocated arrays in JavaScript/TypeScript with strings

I have a system that looks like this: interface Data { x: number; y: number; n: string; } const array = Array(100); I've read that in Chrome the V8 engine will allocate objects as C arrays if the array only contains the same type, but is it possible to actually check if my ´array´ object will act...
Frans Bstrom
1

votes
1

answer
671

Views

Async function which resolve a Promise and then returns a boolean?

I have an async function that supposed to return a boolean if an entity exist based on a previous request call. Here is how it looks like: async vertexExists(properties) { const nbVertices = await this.countVertices(properties); if (nbVertices !== 0) { return true; } return false; } Then in another...
Baptiste Arnaud
1

votes
2

answer
710

Views

Angular | Creating an Observable Not Working

In Angular6, I'm creating an Injectable service. I'm trying to initialize an Observable in the constructor, see simplified code below. Problem: The callback function of Observable.create is never executed, and there is no errors. Why is console.log('authObserver created!') never called? import { O...
Vingtoft
1

votes
1

answer
379

Views

How do I fix the following error?

I wrote the following code following rxjs 6 documentation. Im currently running angular 5, rxjs 6 and angularfire2 rc.10. The error I get is [ts] property 'pipe' does not exist on type 'OperatorFunction'. This is the code this.companies$ = combineLatest(this.authService.user$, this.filter$).pipe( s...
Noober
1

votes
1

answer
72

Views

Typescript: Define initial state for object using interface with dictionary of keys

I've defined my interface as follows: export interface FullCache { // keys names are dynamic [cacheId: string]?: SingleCache; } export interface SingleCache { data: string; } so an example FullCache object could look like this: { ca001:{data:'foo'}, ca002:{data:'bar'} } However, I can't figure out...
Our_Benefactors
-1

votes
0

answer
12

Views

How to fix “forChild of undefined”

I've implemented lazy loading on my Angular 4 app, which is localy working without problems, but on production I've got errors Cannot read property 'forChild' of undefined. The thing is I can not track the problem. If I'm going to deploy app 2 times error will occur on different routes each time. I...
HC1122
1

votes
2

answer
747

Views

Kendo UI for Angular custom sorting grid column

I have the following column in a Kendo Grid (UI for Jquery) with a special sort method based on the needs of my customers. field: 'daysLeft', title: 'Accessible', width: 130, sortable: { compare: function (a, b, descending) { if (a.daysLeft == 'Not started') return 1; if (b.daysLeft == 'Not started'...
h3li0s
1

votes
3

answer
2.4k

Views

React with TypeScript: Type '{}' is not assignable to type

I have a component in React that shows a navigation based on a prop. I cannot figure out why I am getting this error when I'm trying to run the application: (19,8): Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly
gfels
1

votes
1

answer
3.7k

Views

Function implementation is missing or not immediately following the declaration, TypeScript class

I got a handwritten array to fill a table in my class, now I'm getting this array's content from a JSON on ngOnInit but it's not structured as I need. So I'm trying to write a function to fill the table array with this new one I'm getting on ngOnInit. The issue is that when I write code outside of...
prevox
1

votes
2

answer
9.5k

Views

How to set value to Form in Angular 6 using FormControl

Group Select {{group.name}} How can I set the value of the field to Select dynamically in the Component? I looked at the docs, https://angular.io/api/forms/FormControlName#use-with-ngmodel but still no example that can help me. Attempt: this.gGroup.get('groupControl').setValue('Select');
chris
1

votes
3

answer
1.3k

Views

How to add Icon inside the Angular material Snackbar in Angular 5

I am new to angular and I am using Angular Material Design for UI. In my application I have a snackbar . Now I want to set an Icon inside the snackbar but I tried some Stackoverflow post I can't set it . code: this.snackBar.open('You are already registered.Please log in.','', { duration: 2000 }); I...
Zhu
1

votes
2

answer
1.8k

Views

Angular Material | Check if dialog is open

I'm using the Angular Material dialog to show a warning message in my app. I need to check if a dialog is already open like this: private _openDialog() { // if (this.dialog.isOpen()) return;
Vingtoft
1

votes
1

answer
264

Views

Optional parameters based on conditional types

Is it possible to make a function have either mandatory or optional parameters based on conditional types in TypeScript? This is what I've got so far: const foo = ( first: T, second: T extends string ? boolean : undefined ) => undefined; foo('foo', true); // ok, as intended foo(2, true); // not ok,...
torkel
1

votes
1

answer
251

Views

Typescript/Apollo: access property from in memory cache

I'm using Apollo Client and am learning Typescript. I have a Mutation component with this update prop: (cache, { data: { createTask } }) => { const allTasks = cache.readQuery({ query: ALL_TASKS }).allTasks; } I get two compilation errors: Object is possibly 'null'. Property 'allTasks' does not exist...
stoebelj
1

votes
2

answer
1.2k

Views

Use node Path module with angular 6

I'm trying to use the module Path in an Angular 6 project. I found this post to fix the issue : https://gist.github.com/niespodd/1fa82da6f8c901d1c33d2fcbb762947d it says to add a script : const fs = require('fs'); const f = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/w...
An-droid
1

votes
3

answer
107

Views

How dependency injection in Angular is useful since it doesn't solve anything apparently?

How this can improve my code? How it can remove dependency ? I still don't see any benefit of using this ? @Component({ selector: 'talk-list', templateUrl: 'talks.html', providers: [TalksAppBackend] }) class TalkList { constructor(backend:TalksAppBackend) { this.talks = backend.fetchTalks();...
Jameela Khan
1

votes
1

answer
69

Views

How to assign Enum to variable?

The title says it all: how do I assign an Enum to a local variable like: export enum MyEnum { TOP = 'top', RIGHT = 'right', BOTTOM = 'bottom', LEFT = 'left' }; const myEnum: MyEnum = MyEnum; //
scipper
1

votes
1

answer
103

Views

TypeScript: is not assignable to boolean or PromiseLike

I'm writing a React Native app using TypeScript? I have written a function wrapper for functions from React Native Firebase like this: export const checkPermissions = (): Promise => new Promise((resolve, reject) => { firebase .messaging() .hasPermission() .then(enabled => { if (enabled) { return res...
J. Hesters
1

votes
1

answer
781

Views

Angular Karma not capturing browser

I've an existing project on Angular 6 where tests not running. How it happens: in PhpStorm I press in context menu on file run file.specs.ts. It starts test, opens the browser and nothing is happening then. Here is the log Waiting for a captured browser... To capture a browser open http://localhost...
Sergey
1

votes
2

answer
82

Views

Generic typing a hashmap with dynamic key and T array

I try to create a strong typing interface for an hashmap in typescript. The hashmap contains a key with a dynamic string name. And a values which contains an array of Generic type. I tried to use the following interface: export interface DynamicHashmap { [dynamicKey: string]: string; values: T[]; }...
BlackHoleGalaxy
1

votes
1

answer
659

Views

Angular 4 error “Expected 2-3 arguments, but got 1”

I have created a code that I want to upload files uploadImages(fda){ return this.http.post(this.apiUrl+'/uploadImages/'+fda) .map((res : Response) => res); } but shows the error of ERROR in E:/MCQ/CsExamsModule - Client/src/app/services/question.service.ts (90,14): Expected 2-3 arguments, but got 1...
doc leo
1

votes
1

answer
302

Views

In Angular, how do I get the row index on a Mat-Table with expandable content?

I have a page that uses a mat-table with expandable content. I need to be able to have a click event that records the row number of the table that I am clicking on. Now, if I have a table without the expandable content I can successfully use the following in the html file: and the following in the c...
Woody
1

votes
2

answer
683

Views

Angular 5 Unit testing throwing error for “components should create ” test case

I am trying Angular 5 unit testing for the first time. While I already created the app then decided to run testing in it. But I am getting these errors: AppComponent should create the app AppComponent should have as title 'app' AppComponent should render title in a h1 tag GalleryComponent should cr...
vietngogs
1

votes
2

answer
1.4k

Views

Get previous route Angular 7

I created a little service where I can store the route changes. import { Injectable } from '@angular/core'; import { Router, NavigationEnd } from '@angular/router'; @Injectable() export class RouteState { private previousUrl: string; private currentUrl: string; constructor(private router: Router) {...
Testador
1

votes
1

answer
288

Views

Nativescript: HTTP failure response for unknown URL

I have a situation, I have pulled the latest changes from my branch and trying to run the application using tns run android command. The app is launched successfully but the API call is restricted throwing an error as soon as the app starts. I have also gone through this answer but that's not the pr...
Yashwardhan Pauranik
1

votes
2

answer
2.4k

Views

Return when subscribing. … Type 'Subscription' is missing the following properties

I am subscribing a response from an Angular service: books: BookModel[] = []; constructor(private bookService: BookService) { } ngOnInit() { this.books = this.getBooks(); } getBooks(): BookModel[] { return this.bookService.getByCategoryId(1).subscribe((payload: Payload) => { return payload.result.ma...
Miguel Moura
0

votes
0

answer
6

Views

How to use paths in Typescript in fs.readFile and other non-import paths

Say I have the following tsconfig.json 'compilerOptions': { 'baseUrl': 'src', 'paths': { 'app/*': ['src/*'], } ... And I have the following folder structure src/ content/ numbers.csv long/ folder/ path/ to/ file.ts And now I want to use fs.readFileSync to read numbers.csv. But I don't want to do so...
Kousha
1

votes
1

answer
212

Views

mongoose virtuals with typescript error - The containing arrow function captures the global value of 'this' which implicitly has type 'any'

I am trying to use mongoose virtuals with typescript. And not able to make it work. I get error related to using this in the code. I checked many references but could not find a reference to make this work. '[ts] The containing arrow function captures the global value of 'this' which implicitly has...
Dhaval Chokshi
1

votes
1

answer
108

Views

empty JSON parameters for array POST in swagger

I'm using nest.js and I have a post route to add new news to database so I used postman to send array of objects like this: [ { 'newsTitle' : 'title1', 'newsDescription': 'description1' }, { 'newsTitle' : 'title2', 'newsDescription': 'description2' } ] and this the code for post in my controller:...
GNDevs

View additional questions