Posts

Showing posts with the label typescript

What does the “as const” mean in TypeScript and what is its use case?

5 I am confused about the as const cast. I checked a few documents and videos but did not understand it fully. My concern is what does the as const mean in the code below and what is the benefit of using it? const args = [8, 5] as const; const angle = Math.atan2(...args); console.log(angle); typescript type-assertion Share Improve this question Follow edited Apr...

'yield' expression implicitly results in an 'any' type because its containing generator lacks a return-type annotation

3 2 The first snippet is the code im working with and below is the error it throws and it happens on every "yield select" portion that is in the code and im not sure what my next step is. function* onLoadingDomainsresult() { const pathname = yield select(getPathname); interface Params { hastag: string; } 'yield' expression implicitly results in an 'any' type because its containing generator lacks a return-type annotation. TS7057 113 | 114 | function* onLoadingDomainsresult() { > 115 | const pathname = yield select(getPathname); | ^ 116 | 117 | interface Params { 118 | hastag: string; javascript reac...

Typescript infinite recursion reasoning

7 1 Check this typescript 4.2 snippet I found somewhere (playground here): type BlackMagic<T> = { [K in keyof T]: BlackMagic<T[K]> } declare const foo: BlackMagic<{q: string}>; declare const str: BlackMagic<string>; declare const num: BlackMagic<12>; I can't wrap my head around it. How does TS handle this? How is it not stuck in an infinite recursion? Specifically, in the case of str and num , hovering over the variables shows that TS is resolving the types to just string and 12 . How's that even happening? typescript Share ...

Function in javascript file can't detect its own type with declaration file

12 0 Is it possible for a file that has a corresponding declaration file to detect its own type in vscode? Other files can get the typings, but not the file itself. note the any but this has proper typing Reproduction Create a folder Run npm init Hit enter through everything Create 3 files index.js index.d.ts import.js index.js async function test(input) { return null } index.d.ts type input_type = { name: string, salary: number } export function test(input : input_type) : Promise<null> import.js import * as t from './index' t.test() When you mouse over the test function in index.js it doesn't know its parameter type. But in import.js it knows it. How can I get the index.js test function to know its own type. ...