typescript2.0

Rename keys of an object/interface typesafe

吃可爱长大的小学妹 提交于 2021-02-19 04:19:58
问题 I want to dynamically rename keys with a mapping function. For this, I've made this: interface DateRange { startDate: string; endDate: string; } function format<K extends string>(range: DateRange, mapping: {[X in keyof DateRange]: K}): {[P in K]: string} { return { [mapping.startDate]: range.startDate, [mapping.endDate]: range.endDate, }; } When I cast the return value of this function with as {[P in K]: string} it all works fine, but without the cast it doesn't compile. Error Message: TS2322

Typescript: Use class as interface

雨燕双飞 提交于 2021-02-17 19:01:07
问题 I tried to use an Class as Interface for another class. With this I am trying to accomplish an up-to-date mockClass for Testing. This should be possible and is an favorable approach, as state in https://angular.io/guide/styleguide#interfaces And is demonstrate here: Export class as interface in Angular2 But strangely I'm getting an Error using Typescript 2.5.3 in VSCode 1.17.2 Error in class SpecialTest [ts] Class 'SpecialTest' incorrectly implements interface 'Test'. Types have separate

async/await __generator is not defined

旧街凉风 提交于 2021-02-08 15:16:44
问题 I am trying to use async/await functions with angular2-webpack-starer and typescript (which now have support for this functions targeting es5), but I'm getting error: This is code inside component: // function inside component async checkSlug(slug: string) { // nothing here } I am using webpack2.2 and typescript 2.1.5. This is my tsconfig: { "compilerOptions": { "target": "es5", "module": "commonjs", "moduleResolution": "node", "emitDecoratorMetadata": true, "experimentalDecorators": true,

Transpile a TypeScript file in memory

被刻印的时光 ゝ 提交于 2021-02-08 10:01:55
问题 I am looking for a CLI tool that can run a .ts file directly, without writing to a file. That would mean it transpiles in memory, and then somehow passes that data to the node.js executable. Does such an executable exist? 回答1: Does such an executable exist? Yes. Its called ts-node : https://github.com/TypeStrong/ts-node 来源: https://stackoverflow.com/questions/44752995/transpile-a-typescript-file-in-memory

Transpile a TypeScript file in memory

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-08 10:01:34
问题 I am looking for a CLI tool that can run a .ts file directly, without writing to a file. That would mean it transpiles in memory, and then somehow passes that data to the node.js executable. Does such an executable exist? 回答1: Does such an executable exist? Yes. Its called ts-node : https://github.com/TypeStrong/ts-node 来源: https://stackoverflow.com/questions/44752995/transpile-a-typescript-file-in-memory

Angular 4.3.3 HttpClient : How get value from the header of a response?

那年仲夏 提交于 2021-01-29 20:20:22
问题 ( Editor: VS Code; Typescript: 2.2.1 ) The purpose is to get the headers of the response of the request Assume a POST request with HttpClient in a Service import { Injectable } from "@angular/core"; import { HttpClient, HttpHeaders, } from "@angular/common/http"; @Injectable() export class MyHttpClientService { const url = 'url'; const body = { body: 'the body' }; const headers = 'headers made with HttpHeaders'; const options = { headers: headers, observe: "response", // to display the full

Angular 4.3.3 HttpClient : How get value from the header of a response?

懵懂的女人 提交于 2021-01-29 16:21:32
问题 ( Editor: VS Code; Typescript: 2.2.1 ) The purpose is to get the headers of the response of the request Assume a POST request with HttpClient in a Service import { Injectable } from "@angular/core"; import { HttpClient, HttpHeaders, } from "@angular/common/http"; @Injectable() export class MyHttpClientService { const url = 'url'; const body = { body: 'the body' }; const headers = 'headers made with HttpHeaders'; const options = { headers: headers, observe: "response", // to display the full

Getting Typescript 2 paths mapping (aliases) working

╄→гoц情女王★ 提交于 2021-01-29 04:23:23
问题 I am trying to reference custom modules shortcuts (ie use ts paths mapping feature) for my typescript app, with the following config. Project structure dist/ src/ lyrics/ ... ts files app/ ... ts files Full project structure is here: github.com/adadgio/npm-lyrics-ts, dist folder is not commited of course) tsconfig.json { "compilerOptions": { "outDir": "dist", "module": "commonjs", "target": "es6", "sourceMap": true, "moduleResolution": "node", "experimentalDecorators": true,

Getting Typescript 2 paths mapping (aliases) working

只愿长相守 提交于 2021-01-29 04:23:17
问题 I am trying to reference custom modules shortcuts (ie use ts paths mapping feature) for my typescript app, with the following config. Project structure dist/ src/ lyrics/ ... ts files app/ ... ts files Full project structure is here: github.com/adadgio/npm-lyrics-ts, dist folder is not commited of course) tsconfig.json { "compilerOptions": { "outDir": "dist", "module": "commonjs", "target": "es6", "sourceMap": true, "moduleResolution": "node", "experimentalDecorators": true,

TypeScript parent class instance variable return as undefined

不想你离开。 提交于 2021-01-28 06:29:54
问题 I have two typescript classes named Member and another abstract class ComponenetAction . ComponentAction class has Member as instance variable . export abstract class ComponentAction{ public member:Member constructor(){ } setMember(member:Member){ this.member = member; } getMember(){ return this.member; }} Member class export class Member{ membername:String isadmine:String constructor(name:String,admine:String){ this.membername = name; this.isadmine = admine; }} The ComponentAction class is