Typescript: Specify type in object declaration

RainingChain
var a = {
    b:null, 
}

I want to specify that the type of a.b is (num:number) => void while still setting it to null.

Is it possible to do without using class or interface?

Andrew Eisenberg

This should work:

let a = {
  b: <(number) => void> null
};

Or you can use a type declaration to make your special function explicit:

declare type MyFun = (number) => void;
let a = {
  b: <MyFun> null
};

Although it's not necessary, I tend to make use of type declarations in my code when there the function has semantics that is not easily caught in the type signature, but can be specified easily in a name.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TypeScript type declaration

From Dev

typescript parameter to an object declaration

From Dev

Object type declaration

From Dev

Specify the Type on a Class Object

From Dev

Typescript / Angular Type declaration useless?

From Java

TypeScript type declaration for "member should not exist on this type"?

From Java

type declaration type object is not subscriptable list

From Dev

class declaration - Property does not exist on type in TypeScript

From Dev

Use declaration merging to build a type (Typescript)

From Java

Specify return type in TypeScript arrow function

From Dev

How to dynamically specify type in TypeScript generics?

From Dev

How to specify type of index of enum in typescript

From Dev

TypeScript Specify Type For Individual Function Use

From Dev

How to specify Typescript Mongoose model type in await

From Dev

Typescript, Type Object Is Not Generic

From Dev

Typescript: define type of an object

From Dev

Typescript type to object literal

From Dev

How to specify partial object signatures correctly in TypeScript?

From Dev

Why java allows object declaration to not contain the type

From Dev

Get Class Type of Object and use it in a Variable Declaration

From Dev

Sphinx docstring: specify class type instead of object?

From Dev

Can I specify a control for each type of object?

From Java

Type definition in object literal in TypeScript

From Dev

How to type an object key in Typescript?

From Dev

Is this a type declaration?

From Dev

Typescript - object of specified type is not assignable to generic type

From Dev

How can I specify a return type for a $http call with Typescript?

From Java

Can You Specify Multiple Type Constraints For TypeScript Generics

From Dev

How to specify that a function returns a certain type when new'ed in TypeScript?

Related Related

  1. 1

    TypeScript type declaration

  2. 2

    typescript parameter to an object declaration

  3. 3

    Object type declaration

  4. 4

    Specify the Type on a Class Object

  5. 5

    Typescript / Angular Type declaration useless?

  6. 6

    TypeScript type declaration for "member should not exist on this type"?

  7. 7

    type declaration type object is not subscriptable list

  8. 8

    class declaration - Property does not exist on type in TypeScript

  9. 9

    Use declaration merging to build a type (Typescript)

  10. 10

    Specify return type in TypeScript arrow function

  11. 11

    How to dynamically specify type in TypeScript generics?

  12. 12

    How to specify type of index of enum in typescript

  13. 13

    TypeScript Specify Type For Individual Function Use

  14. 14

    How to specify Typescript Mongoose model type in await

  15. 15

    Typescript, Type Object Is Not Generic

  16. 16

    Typescript: define type of an object

  17. 17

    Typescript type to object literal

  18. 18

    How to specify partial object signatures correctly in TypeScript?

  19. 19

    Why java allows object declaration to not contain the type

  20. 20

    Get Class Type of Object and use it in a Variable Declaration

  21. 21

    Sphinx docstring: specify class type instead of object?

  22. 22

    Can I specify a control for each type of object?

  23. 23

    Type definition in object literal in TypeScript

  24. 24

    How to type an object key in Typescript?

  25. 25

    Is this a type declaration?

  26. 26

    Typescript - object of specified type is not assignable to generic type

  27. 27

    How can I specify a return type for a $http call with Typescript?

  28. 28

    Can You Specify Multiple Type Constraints For TypeScript Generics

  29. 29

    How to specify that a function returns a certain type when new'ed in TypeScript?

HotTag

Archive