天道酬勤,学无止境

typescript3.0

Get type completion based on dynamic (mapped/conditional) type

You can drop the following code into a foo.ts file. I am trying to dynamically generate types. What I am doing is based off this question: Map array to an interface type TypeMapping = { Boolean: boolean, String: string, Number: number, ArrayOfString: Array<string>, } export enum Type { Boolean = 'Boolean', String = 'String', Number = 'Number', ArrayOfString = 'ArrayOfString' } const asOptions = <K extends Array<string>, T extends Array<{ name: K, type: keyof TypeMapping }>>(t: T) => t; type OptionsToType<T extends Array<{ name: Array<string>, type: keyof TypeMapping }>> = { [K in T[number][

2021-07-03 09:34:23    分类:问答    typescript   tsc   typescript3.0

“Cannot find module” error when using TypeScript 3 Project References

I'm trying to get TypeScript 3's project references working but struggling to import a function from a referenced project. I have a ProjectA that references Shared. Here's the file structure: ProjectA |_ src | |_person.ts |_ tsconfig.json | Shared |_ src |_utils.ts |_ tsconfig.json Here's utils.ts: export function guid() { return "a guid"; } Here's Shared/tsconfig.json: { "compilerOptions": { "composite": true, "declaration": true, "target": "es5", "outDir": "dist", "module": "es6", "moduleResolution": "node", "sourceMap": true, "noImplicitReturns": true, "noImplicitAny": true }, "include": [

2021-06-05 17:53:08    分类:问答    typescript   typescript3.0

Conditional types with TypeScript

Say I have this: type TypeMapping = { Boolean: boolean, String: string, Number: number, ArrayOfString: Array<string>, ArrayOfBoolean: Array<boolean> } export interface ElemType { foo: keyof TypeMapping, default: valueof TypeMapping } instead of using any for default, I want to conditionally define it, I tried this: export interface ElemType<T extends TypeMapping> { foo: keyof T, default: T } but that doesn't seem quite right, does anyone know the right way to do this? if it's not clear, for any given object that has type ElemType, the key that foo points to, must be matched by the value that

2021-04-30 05:13:39    分类:问答    typescript   tsc   typescript3.0

Do I ever need explicit allowSyntheticDefaultImports if esModuleInterop is true configuring TypeScript transpilation?

I need confirmation on the following theory. According to TS docs, there are two options that can be set in tsconfig.json. --allowSyntheticDefaultImports: Allow default imports from modules with no default export. This does not affect code emit, just typechecking. --esModuleInterop: Emit __importStar and __importDefault helpers for runtime babel ecosystem compatibility and enable --allowSyntheticDefaultImports for typesystem compatibility. When I google around, I see both being set to true (at least in regard to the behavior I'm aiming at). However, as far I understand the docs, TS and

2021-04-26 15:05:02    分类:问答    javascript   typescript   webpack-4   typescript3.0

cannot find name 'require' in angular 7(typescript 3.1.3)

My question is why this error shown? ERROR in src/app/p2p/p2p.component.ts(14,16): error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try npm i @types/node. I have install @types/node in app/tsconfig.app.json have add "types": [ "node" ], "typeRoots": [ "../node_modules/@types" ] but there is error cannot find 'require'

2021-04-09 07:41:29    分类:问答    angular   typescript   angular7   typescript3.0

“未知”与“任何”('unknown' vs. 'any')

问题 根据他们的Wiki,TypeScript 3.0引入了unknown类型: 未知现在是保留类型名称,因为它现在是内置类型。 根据未知的预期用途,您可能希望完全删除声明(支持新引入的未知类型),或将其重命名为其他名称。 unknown和any和any什么不一样? 我们什么时候应该使用unknown过any ? 回答1 您可以在PR或RC公告中阅读有关unknown更多信息,但要点是: [..] unknown是任何类型安全的对应物。 任何东西都可以分配给未知对象,但是除了自身以及没有类型声明或基于控制流的缩小对象之外,任何东西都不能分配给其他对象。 同样,在未先声明或缩小为更具体的类型之前,不允许对未知数执行任何操作。 几个例子: let vAny: any = 10; // We can assign anything to any let vUnknown: unknown = 10; // We can assign anything to unknown just like any let s1: string = vAny; // Any is assignable to anything let s2: string = vUnknown; // Invalid we can't assign vUnknown to any other type (without

2021-03-31 08:42:42    分类:技术分享    typescript   typescript3.0

'unknown' vs. 'any'

TypeScript 3.0 introduces unknown type, according to their wiki: unknown is now a reserved type name, as it is now a built-in type. Depending on your intended use of unknown, you may want to remove the declaration entirely (favoring the newly introduced unknown type), or rename it to something else. What is difference between unknown and any? When should we use unknown over any?

2021-03-22 06:33:35    分类:问答    typescript   typescript3.0