TypeScript. How to use not exported type definitions?
up vote
3
down vote
favorite
Just look at this typescript code:
lib.ts
interface Human
name: string;
age: number;
export default class HumanFactory
getHuman(): Human
return
name: "John",
age: 22,
index.ts
import HumanFactory from "./lib";
export class Foo
human: any;
constructor()
const factory = new HumanFactory();
this.human = factory.getHuman();
diffWithError(age: number): number
return age - this.human.name;
diffWithTypingAndAutocoplete(age: number): number
const factory = new HumanFactory();
return age - factory.getHuman().name;
The problem in "human" property of "Foo" class. I can't define type of this variable as "Human" interface from lib.ts.
In method "diffWithError" I make an error - use number "age" and string "name" in arithmetic operation, but neither IDE nor ts compiler know about this, because in this context, type of "this.human.name" is "any"
In method "diffWithTypingAndAutocoplete" I just use method "getHuman". IDE and compiler know about type of method result. This is "Human" interface and field "name" are "string". This method trigger an error when compiling sources.
I found this problem when I tried import .d.ts file of JS lib and I don't have ability to export needed interface. Can I somehow define valid type of "human" property without copy and paste code of "Human" interface whenever i want to define type (and without inline type definitions, like name: string, age: number ).
I don not want to create instances of not exported classes, I just want type checking and autocomplete.
P.S. I try write this:
human: Human
compiler trigger an error: "error TS2304: Cannot find name 'Human'" (expected behavior)
P.S.S I try to do this with triple slash directive:
///<reference path="./lib.ts" />
but this not working too.
Sorry for my poor english and thanks for answers
typescript typescript-typings
add a comment |
up vote
3
down vote
favorite
Just look at this typescript code:
lib.ts
interface Human
name: string;
age: number;
export default class HumanFactory
getHuman(): Human
return
name: "John",
age: 22,
index.ts
import HumanFactory from "./lib";
export class Foo
human: any;
constructor()
const factory = new HumanFactory();
this.human = factory.getHuman();
diffWithError(age: number): number
return age - this.human.name;
diffWithTypingAndAutocoplete(age: number): number
const factory = new HumanFactory();
return age - factory.getHuman().name;
The problem in "human" property of "Foo" class. I can't define type of this variable as "Human" interface from lib.ts.
In method "diffWithError" I make an error - use number "age" and string "name" in arithmetic operation, but neither IDE nor ts compiler know about this, because in this context, type of "this.human.name" is "any"
In method "diffWithTypingAndAutocoplete" I just use method "getHuman". IDE and compiler know about type of method result. This is "Human" interface and field "name" are "string". This method trigger an error when compiling sources.
I found this problem when I tried import .d.ts file of JS lib and I don't have ability to export needed interface. Can I somehow define valid type of "human" property without copy and paste code of "Human" interface whenever i want to define type (and without inline type definitions, like name: string, age: number ).
I don not want to create instances of not exported classes, I just want type checking and autocomplete.
P.S. I try write this:
human: Human
compiler trigger an error: "error TS2304: Cannot find name 'Human'" (expected behavior)
P.S.S I try to do this with triple slash directive:
///<reference path="./lib.ts" />
but this not working too.
Sorry for my poor english and thanks for answers
typescript typescript-typings
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Just look at this typescript code:
lib.ts
interface Human
name: string;
age: number;
export default class HumanFactory
getHuman(): Human
return
name: "John",
age: 22,
index.ts
import HumanFactory from "./lib";
export class Foo
human: any;
constructor()
const factory = new HumanFactory();
this.human = factory.getHuman();
diffWithError(age: number): number
return age - this.human.name;
diffWithTypingAndAutocoplete(age: number): number
const factory = new HumanFactory();
return age - factory.getHuman().name;
The problem in "human" property of "Foo" class. I can't define type of this variable as "Human" interface from lib.ts.
In method "diffWithError" I make an error - use number "age" and string "name" in arithmetic operation, but neither IDE nor ts compiler know about this, because in this context, type of "this.human.name" is "any"
In method "diffWithTypingAndAutocoplete" I just use method "getHuman". IDE and compiler know about type of method result. This is "Human" interface and field "name" are "string". This method trigger an error when compiling sources.
I found this problem when I tried import .d.ts file of JS lib and I don't have ability to export needed interface. Can I somehow define valid type of "human" property without copy and paste code of "Human" interface whenever i want to define type (and without inline type definitions, like name: string, age: number ).
I don not want to create instances of not exported classes, I just want type checking and autocomplete.
P.S. I try write this:
human: Human
compiler trigger an error: "error TS2304: Cannot find name 'Human'" (expected behavior)
P.S.S I try to do this with triple slash directive:
///<reference path="./lib.ts" />
but this not working too.
Sorry for my poor english and thanks for answers
typescript typescript-typings
Just look at this typescript code:
lib.ts
interface Human
name: string;
age: number;
export default class HumanFactory
getHuman(): Human
return
name: "John",
age: 22,
index.ts
import HumanFactory from "./lib";
export class Foo
human: any;
constructor()
const factory = new HumanFactory();
this.human = factory.getHuman();
diffWithError(age: number): number
return age - this.human.name;
diffWithTypingAndAutocoplete(age: number): number
const factory = new HumanFactory();
return age - factory.getHuman().name;
The problem in "human" property of "Foo" class. I can't define type of this variable as "Human" interface from lib.ts.
In method "diffWithError" I make an error - use number "age" and string "name" in arithmetic operation, but neither IDE nor ts compiler know about this, because in this context, type of "this.human.name" is "any"
In method "diffWithTypingAndAutocoplete" I just use method "getHuman". IDE and compiler know about type of method result. This is "Human" interface and field "name" are "string". This method trigger an error when compiling sources.
I found this problem when I tried import .d.ts file of JS lib and I don't have ability to export needed interface. Can I somehow define valid type of "human" property without copy and paste code of "Human" interface whenever i want to define type (and without inline type definitions, like name: string, age: number ).
I don not want to create instances of not exported classes, I just want type checking and autocomplete.
P.S. I try write this:
human: Human
compiler trigger an error: "error TS2304: Cannot find name 'Human'" (expected behavior)
P.S.S I try to do this with triple slash directive:
///<reference path="./lib.ts" />
but this not working too.
Sorry for my poor english and thanks for answers
typescript typescript-typings
typescript typescript-typings
asked Oct 15 '17 at 12:22
Nazik Orl
484
484
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
0
down vote
accepted
If you can't change lib.ts you can "query" return type of getHuman
function. It is a bit tricky because typescript currently doesn't provide any straight forward method for this:
import HumanFactory from "./lib";
const dummyHuman = !true && new HumanFactory().getHuman();
type Human = typeof dummyHuman;
export class Foo
human: Human;
// ...
!true &&
is used to prevent new HumanFactory().getHuman()
execution.
Unfortunately it does not work:error TS2322: Type 'Human' is not assignable to type 'false'.
triggered when I try thisthis.human = factory.getHuman()
in constructor
– Nazik Orl
Oct 16 '17 at 16:08
Which version of typescript do you use?
– Aleksey L.
Oct 17 '17 at 5:29
add a comment |
up vote
3
down vote
I found a solution!
I make file human-interface.ts with this content:
import HumanFactory from './lib';
const humanObject = new HumanFactory().getHuman();
type HumanType = typeof humanObject;
export default interface Human extends HumanType
Import of this interface in main file not execute creation of "HumanFactory" and type checking work properly.
Thanks for idea with typeof
add a comment |
up vote
1
down vote
You need to export Human
so that it is visible - and usable - from index.ts
as well (as HumanFactory
). Do not use default exports but "named exports" i.e. try this
export interface Human
name: string;
age: number;
export class HumanFactory
getHuman(): Human
return
name: "John",
age: 22,
In index.ts
import Human, HumanFactory from "./lib";
** EDIT **
If you cannot change lib.d.ts
then redefine Human
and use double-casting i.e.
import HumanFactory from "./lib";
interface Human
name: string;
age: number;
export class Foo
human: Human; // <= change here
constructor()
const factory = new HumanFactory();
this.human = factory.getHuman() as any as Human; // <= double casting
diffWithError(age: number): number
return age - this.human.name;
diffWithTypingAndAutocoplete(age: number): number
const factory = new HumanFactory();
return age - factory.getHuman().name;
I can't editlib.ts
. In my project this is type definition of external library in .d.ts file.
– Nazik Orl
Oct 16 '17 at 16:16
@NazikOrl see my edits for an alternative solution
– Bruno Grieder
Oct 16 '17 at 16:32
Thanks for the answer. I found another solution acceptable for me
– Nazik Orl
Oct 16 '17 at 16:39
add a comment |
up vote
0
down vote
This was made easier with the introduction of the ReturnType<>
static type in TypeScript 2.8.
import HumanFactory from "./lib";
type Human = ReturnType<typeof HumanFactory.prototype.getHuman>
See also https://stackoverflow.com/a/43053162
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46754984%2ftypescript-how-to-use-not-exported-type-definitions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
If you can't change lib.ts you can "query" return type of getHuman
function. It is a bit tricky because typescript currently doesn't provide any straight forward method for this:
import HumanFactory from "./lib";
const dummyHuman = !true && new HumanFactory().getHuman();
type Human = typeof dummyHuman;
export class Foo
human: Human;
// ...
!true &&
is used to prevent new HumanFactory().getHuman()
execution.
Unfortunately it does not work:error TS2322: Type 'Human' is not assignable to type 'false'.
triggered when I try thisthis.human = factory.getHuman()
in constructor
– Nazik Orl
Oct 16 '17 at 16:08
Which version of typescript do you use?
– Aleksey L.
Oct 17 '17 at 5:29
add a comment |
up vote
0
down vote
accepted
If you can't change lib.ts you can "query" return type of getHuman
function. It is a bit tricky because typescript currently doesn't provide any straight forward method for this:
import HumanFactory from "./lib";
const dummyHuman = !true && new HumanFactory().getHuman();
type Human = typeof dummyHuman;
export class Foo
human: Human;
// ...
!true &&
is used to prevent new HumanFactory().getHuman()
execution.
Unfortunately it does not work:error TS2322: Type 'Human' is not assignable to type 'false'.
triggered when I try thisthis.human = factory.getHuman()
in constructor
– Nazik Orl
Oct 16 '17 at 16:08
Which version of typescript do you use?
– Aleksey L.
Oct 17 '17 at 5:29
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
If you can't change lib.ts you can "query" return type of getHuman
function. It is a bit tricky because typescript currently doesn't provide any straight forward method for this:
import HumanFactory from "./lib";
const dummyHuman = !true && new HumanFactory().getHuman();
type Human = typeof dummyHuman;
export class Foo
human: Human;
// ...
!true &&
is used to prevent new HumanFactory().getHuman()
execution.
If you can't change lib.ts you can "query" return type of getHuman
function. It is a bit tricky because typescript currently doesn't provide any straight forward method for this:
import HumanFactory from "./lib";
const dummyHuman = !true && new HumanFactory().getHuman();
type Human = typeof dummyHuman;
export class Foo
human: Human;
// ...
!true &&
is used to prevent new HumanFactory().getHuman()
execution.
answered Oct 16 '17 at 6:09
Aleksey L.
11k2433
11k2433
Unfortunately it does not work:error TS2322: Type 'Human' is not assignable to type 'false'.
triggered when I try thisthis.human = factory.getHuman()
in constructor
– Nazik Orl
Oct 16 '17 at 16:08
Which version of typescript do you use?
– Aleksey L.
Oct 17 '17 at 5:29
add a comment |
Unfortunately it does not work:error TS2322: Type 'Human' is not assignable to type 'false'.
triggered when I try thisthis.human = factory.getHuman()
in constructor
– Nazik Orl
Oct 16 '17 at 16:08
Which version of typescript do you use?
– Aleksey L.
Oct 17 '17 at 5:29
Unfortunately it does not work:
error TS2322: Type 'Human' is not assignable to type 'false'.
triggered when I try this this.human = factory.getHuman()
in constructor– Nazik Orl
Oct 16 '17 at 16:08
Unfortunately it does not work:
error TS2322: Type 'Human' is not assignable to type 'false'.
triggered when I try this this.human = factory.getHuman()
in constructor– Nazik Orl
Oct 16 '17 at 16:08
Which version of typescript do you use?
– Aleksey L.
Oct 17 '17 at 5:29
Which version of typescript do you use?
– Aleksey L.
Oct 17 '17 at 5:29
add a comment |
up vote
3
down vote
I found a solution!
I make file human-interface.ts with this content:
import HumanFactory from './lib';
const humanObject = new HumanFactory().getHuman();
type HumanType = typeof humanObject;
export default interface Human extends HumanType
Import of this interface in main file not execute creation of "HumanFactory" and type checking work properly.
Thanks for idea with typeof
add a comment |
up vote
3
down vote
I found a solution!
I make file human-interface.ts with this content:
import HumanFactory from './lib';
const humanObject = new HumanFactory().getHuman();
type HumanType = typeof humanObject;
export default interface Human extends HumanType
Import of this interface in main file not execute creation of "HumanFactory" and type checking work properly.
Thanks for idea with typeof
add a comment |
up vote
3
down vote
up vote
3
down vote
I found a solution!
I make file human-interface.ts with this content:
import HumanFactory from './lib';
const humanObject = new HumanFactory().getHuman();
type HumanType = typeof humanObject;
export default interface Human extends HumanType
Import of this interface in main file not execute creation of "HumanFactory" and type checking work properly.
Thanks for idea with typeof
I found a solution!
I make file human-interface.ts with this content:
import HumanFactory from './lib';
const humanObject = new HumanFactory().getHuman();
type HumanType = typeof humanObject;
export default interface Human extends HumanType
Import of this interface in main file not execute creation of "HumanFactory" and type checking work properly.
Thanks for idea with typeof
answered Oct 16 '17 at 16:31
Nazik Orl
484
484
add a comment |
add a comment |
up vote
1
down vote
You need to export Human
so that it is visible - and usable - from index.ts
as well (as HumanFactory
). Do not use default exports but "named exports" i.e. try this
export interface Human
name: string;
age: number;
export class HumanFactory
getHuman(): Human
return
name: "John",
age: 22,
In index.ts
import Human, HumanFactory from "./lib";
** EDIT **
If you cannot change lib.d.ts
then redefine Human
and use double-casting i.e.
import HumanFactory from "./lib";
interface Human
name: string;
age: number;
export class Foo
human: Human; // <= change here
constructor()
const factory = new HumanFactory();
this.human = factory.getHuman() as any as Human; // <= double casting
diffWithError(age: number): number
return age - this.human.name;
diffWithTypingAndAutocoplete(age: number): number
const factory = new HumanFactory();
return age - factory.getHuman().name;
I can't editlib.ts
. In my project this is type definition of external library in .d.ts file.
– Nazik Orl
Oct 16 '17 at 16:16
@NazikOrl see my edits for an alternative solution
– Bruno Grieder
Oct 16 '17 at 16:32
Thanks for the answer. I found another solution acceptable for me
– Nazik Orl
Oct 16 '17 at 16:39
add a comment |
up vote
1
down vote
You need to export Human
so that it is visible - and usable - from index.ts
as well (as HumanFactory
). Do not use default exports but "named exports" i.e. try this
export interface Human
name: string;
age: number;
export class HumanFactory
getHuman(): Human
return
name: "John",
age: 22,
In index.ts
import Human, HumanFactory from "./lib";
** EDIT **
If you cannot change lib.d.ts
then redefine Human
and use double-casting i.e.
import HumanFactory from "./lib";
interface Human
name: string;
age: number;
export class Foo
human: Human; // <= change here
constructor()
const factory = new HumanFactory();
this.human = factory.getHuman() as any as Human; // <= double casting
diffWithError(age: number): number
return age - this.human.name;
diffWithTypingAndAutocoplete(age: number): number
const factory = new HumanFactory();
return age - factory.getHuman().name;
I can't editlib.ts
. In my project this is type definition of external library in .d.ts file.
– Nazik Orl
Oct 16 '17 at 16:16
@NazikOrl see my edits for an alternative solution
– Bruno Grieder
Oct 16 '17 at 16:32
Thanks for the answer. I found another solution acceptable for me
– Nazik Orl
Oct 16 '17 at 16:39
add a comment |
up vote
1
down vote
up vote
1
down vote
You need to export Human
so that it is visible - and usable - from index.ts
as well (as HumanFactory
). Do not use default exports but "named exports" i.e. try this
export interface Human
name: string;
age: number;
export class HumanFactory
getHuman(): Human
return
name: "John",
age: 22,
In index.ts
import Human, HumanFactory from "./lib";
** EDIT **
If you cannot change lib.d.ts
then redefine Human
and use double-casting i.e.
import HumanFactory from "./lib";
interface Human
name: string;
age: number;
export class Foo
human: Human; // <= change here
constructor()
const factory = new HumanFactory();
this.human = factory.getHuman() as any as Human; // <= double casting
diffWithError(age: number): number
return age - this.human.name;
diffWithTypingAndAutocoplete(age: number): number
const factory = new HumanFactory();
return age - factory.getHuman().name;
You need to export Human
so that it is visible - and usable - from index.ts
as well (as HumanFactory
). Do not use default exports but "named exports" i.e. try this
export interface Human
name: string;
age: number;
export class HumanFactory
getHuman(): Human
return
name: "John",
age: 22,
In index.ts
import Human, HumanFactory from "./lib";
** EDIT **
If you cannot change lib.d.ts
then redefine Human
and use double-casting i.e.
import HumanFactory from "./lib";
interface Human
name: string;
age: number;
export class Foo
human: Human; // <= change here
constructor()
const factory = new HumanFactory();
this.human = factory.getHuman() as any as Human; // <= double casting
diffWithError(age: number): number
return age - this.human.name;
diffWithTypingAndAutocoplete(age: number): number
const factory = new HumanFactory();
return age - factory.getHuman().name;
edited Oct 16 '17 at 16:31
answered Oct 15 '17 at 17:10
Bruno Grieder
14.7k23769
14.7k23769
I can't editlib.ts
. In my project this is type definition of external library in .d.ts file.
– Nazik Orl
Oct 16 '17 at 16:16
@NazikOrl see my edits for an alternative solution
– Bruno Grieder
Oct 16 '17 at 16:32
Thanks for the answer. I found another solution acceptable for me
– Nazik Orl
Oct 16 '17 at 16:39
add a comment |
I can't editlib.ts
. In my project this is type definition of external library in .d.ts file.
– Nazik Orl
Oct 16 '17 at 16:16
@NazikOrl see my edits for an alternative solution
– Bruno Grieder
Oct 16 '17 at 16:32
Thanks for the answer. I found another solution acceptable for me
– Nazik Orl
Oct 16 '17 at 16:39
I can't edit
lib.ts
. In my project this is type definition of external library in .d.ts file.– Nazik Orl
Oct 16 '17 at 16:16
I can't edit
lib.ts
. In my project this is type definition of external library in .d.ts file.– Nazik Orl
Oct 16 '17 at 16:16
@NazikOrl see my edits for an alternative solution
– Bruno Grieder
Oct 16 '17 at 16:32
@NazikOrl see my edits for an alternative solution
– Bruno Grieder
Oct 16 '17 at 16:32
Thanks for the answer. I found another solution acceptable for me
– Nazik Orl
Oct 16 '17 at 16:39
Thanks for the answer. I found another solution acceptable for me
– Nazik Orl
Oct 16 '17 at 16:39
add a comment |
up vote
0
down vote
This was made easier with the introduction of the ReturnType<>
static type in TypeScript 2.8.
import HumanFactory from "./lib";
type Human = ReturnType<typeof HumanFactory.prototype.getHuman>
See also https://stackoverflow.com/a/43053162
add a comment |
up vote
0
down vote
This was made easier with the introduction of the ReturnType<>
static type in TypeScript 2.8.
import HumanFactory from "./lib";
type Human = ReturnType<typeof HumanFactory.prototype.getHuman>
See also https://stackoverflow.com/a/43053162
add a comment |
up vote
0
down vote
up vote
0
down vote
This was made easier with the introduction of the ReturnType<>
static type in TypeScript 2.8.
import HumanFactory from "./lib";
type Human = ReturnType<typeof HumanFactory.prototype.getHuman>
See also https://stackoverflow.com/a/43053162
This was made easier with the introduction of the ReturnType<>
static type in TypeScript 2.8.
import HumanFactory from "./lib";
type Human = ReturnType<typeof HumanFactory.prototype.getHuman>
See also https://stackoverflow.com/a/43053162
answered Nov 9 at 16:15
Kjetil Limkjær
1,2101022
1,2101022
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46754984%2ftypescript-how-to-use-not-exported-type-definitions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown