Private method in Typescript definitions(React)
This is just a general question which has been bothering me for sometime.
I have a React component that has some methods both private and public.
Lets assume I have a component:
import React from 'react';
import { CRComponent } from '../CRComponent';
export interface Props {
className?: string;
type?: string;
}
class AlertItem extends CRComponent<Props> {
public render() {
return (
///// Some render
);
}
private getClassNames() {
///some logic
}
private mapAlertTypeIcon() {
///Some logic
}
}
export default AlertItem;
When I generate a typescript definition file the following file gets generated:
import { CRComponent } from '../CRComponent';
interface Props {
className?: string;
}
declare class AlertItem extends CRComponent<Props> {
private getClassNames;
private mapAlertTypeIcon;
public render(): JSX.Element;
}
export default AlertItem;
I purposefully removed the prop type
from the .d.ts file so that the user cannot use it as it is internal.
Now when I publish this .d.ts file all the member methods are published in .d.ts file including private. For this class the methods are relatively less. Suppose I have a big component then the number of private methods might be huge.
Now a component library will have lots of such component.
When I publish these a major size of the package is because of these files. So my question is that whether it is necessary to keep private members in the .d.ts files or can I just ignore them as the user will not have access to them anyway?
I tried finding answers but could not find one. So a help would be appreciated.
reactjs typescript
add a comment |
This is just a general question which has been bothering me for sometime.
I have a React component that has some methods both private and public.
Lets assume I have a component:
import React from 'react';
import { CRComponent } from '../CRComponent';
export interface Props {
className?: string;
type?: string;
}
class AlertItem extends CRComponent<Props> {
public render() {
return (
///// Some render
);
}
private getClassNames() {
///some logic
}
private mapAlertTypeIcon() {
///Some logic
}
}
export default AlertItem;
When I generate a typescript definition file the following file gets generated:
import { CRComponent } from '../CRComponent';
interface Props {
className?: string;
}
declare class AlertItem extends CRComponent<Props> {
private getClassNames;
private mapAlertTypeIcon;
public render(): JSX.Element;
}
export default AlertItem;
I purposefully removed the prop type
from the .d.ts file so that the user cannot use it as it is internal.
Now when I publish this .d.ts file all the member methods are published in .d.ts file including private. For this class the methods are relatively less. Suppose I have a big component then the number of private methods might be huge.
Now a component library will have lots of such component.
When I publish these a major size of the package is because of these files. So my question is that whether it is necessary to keep private members in the .d.ts files or can I just ignore them as the user will not have access to them anyway?
I tried finding answers but could not find one. So a help would be appreciated.
reactjs typescript
add a comment |
This is just a general question which has been bothering me for sometime.
I have a React component that has some methods both private and public.
Lets assume I have a component:
import React from 'react';
import { CRComponent } from '../CRComponent';
export interface Props {
className?: string;
type?: string;
}
class AlertItem extends CRComponent<Props> {
public render() {
return (
///// Some render
);
}
private getClassNames() {
///some logic
}
private mapAlertTypeIcon() {
///Some logic
}
}
export default AlertItem;
When I generate a typescript definition file the following file gets generated:
import { CRComponent } from '../CRComponent';
interface Props {
className?: string;
}
declare class AlertItem extends CRComponent<Props> {
private getClassNames;
private mapAlertTypeIcon;
public render(): JSX.Element;
}
export default AlertItem;
I purposefully removed the prop type
from the .d.ts file so that the user cannot use it as it is internal.
Now when I publish this .d.ts file all the member methods are published in .d.ts file including private. For this class the methods are relatively less. Suppose I have a big component then the number of private methods might be huge.
Now a component library will have lots of such component.
When I publish these a major size of the package is because of these files. So my question is that whether it is necessary to keep private members in the .d.ts files or can I just ignore them as the user will not have access to them anyway?
I tried finding answers but could not find one. So a help would be appreciated.
reactjs typescript
This is just a general question which has been bothering me for sometime.
I have a React component that has some methods both private and public.
Lets assume I have a component:
import React from 'react';
import { CRComponent } from '../CRComponent';
export interface Props {
className?: string;
type?: string;
}
class AlertItem extends CRComponent<Props> {
public render() {
return (
///// Some render
);
}
private getClassNames() {
///some logic
}
private mapAlertTypeIcon() {
///Some logic
}
}
export default AlertItem;
When I generate a typescript definition file the following file gets generated:
import { CRComponent } from '../CRComponent';
interface Props {
className?: string;
}
declare class AlertItem extends CRComponent<Props> {
private getClassNames;
private mapAlertTypeIcon;
public render(): JSX.Element;
}
export default AlertItem;
I purposefully removed the prop type
from the .d.ts file so that the user cannot use it as it is internal.
Now when I publish this .d.ts file all the member methods are published in .d.ts file including private. For this class the methods are relatively less. Suppose I have a big component then the number of private methods might be huge.
Now a component library will have lots of such component.
When I publish these a major size of the package is because of these files. So my question is that whether it is necessary to keep private members in the .d.ts files or can I just ignore them as the user will not have access to them anyway?
I tried finding answers but could not find one. So a help would be appreciated.
reactjs typescript
reactjs typescript
asked Nov 20 '18 at 9:07


Varun SharmaVarun Sharma
628
628
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
A private method changes whether you can create a substitutable type, for example the declaration of Example
prevents a type match with our concrete class (whether or not we add a private identity member!)
declare class Example {
private identity: string;
public doSomething(): string;
}
class ConcreteExample {
private identity: string = 'id';
doSomething() {
return 'done';
}
}
function passMeExample(input: Example) {
}
// ConcreteExample is not allowable!
passMeExample(new ConcreteExample());
If you remove the private members, it changes the behaviour of the type checking:
declare class Example {
public doSomething(): string;
}
class ConcreteExample {
private identity: string = 'id';
doSomething() {
return 'done';
}
}
function passMeExample(input: Example) {
}
// OK!
passMeExample(new ConcreteExample());
So it is important to bear in mind that there is a difference in behaviour if you remove those private members. The classes will not act as they would if they were included.
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',
autoActivateHeartbeat: false,
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%2f53389553%2fprivate-method-in-typescript-definitionsreact%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A private method changes whether you can create a substitutable type, for example the declaration of Example
prevents a type match with our concrete class (whether or not we add a private identity member!)
declare class Example {
private identity: string;
public doSomething(): string;
}
class ConcreteExample {
private identity: string = 'id';
doSomething() {
return 'done';
}
}
function passMeExample(input: Example) {
}
// ConcreteExample is not allowable!
passMeExample(new ConcreteExample());
If you remove the private members, it changes the behaviour of the type checking:
declare class Example {
public doSomething(): string;
}
class ConcreteExample {
private identity: string = 'id';
doSomething() {
return 'done';
}
}
function passMeExample(input: Example) {
}
// OK!
passMeExample(new ConcreteExample());
So it is important to bear in mind that there is a difference in behaviour if you remove those private members. The classes will not act as they would if they were included.
add a comment |
A private method changes whether you can create a substitutable type, for example the declaration of Example
prevents a type match with our concrete class (whether or not we add a private identity member!)
declare class Example {
private identity: string;
public doSomething(): string;
}
class ConcreteExample {
private identity: string = 'id';
doSomething() {
return 'done';
}
}
function passMeExample(input: Example) {
}
// ConcreteExample is not allowable!
passMeExample(new ConcreteExample());
If you remove the private members, it changes the behaviour of the type checking:
declare class Example {
public doSomething(): string;
}
class ConcreteExample {
private identity: string = 'id';
doSomething() {
return 'done';
}
}
function passMeExample(input: Example) {
}
// OK!
passMeExample(new ConcreteExample());
So it is important to bear in mind that there is a difference in behaviour if you remove those private members. The classes will not act as they would if they were included.
add a comment |
A private method changes whether you can create a substitutable type, for example the declaration of Example
prevents a type match with our concrete class (whether or not we add a private identity member!)
declare class Example {
private identity: string;
public doSomething(): string;
}
class ConcreteExample {
private identity: string = 'id';
doSomething() {
return 'done';
}
}
function passMeExample(input: Example) {
}
// ConcreteExample is not allowable!
passMeExample(new ConcreteExample());
If you remove the private members, it changes the behaviour of the type checking:
declare class Example {
public doSomething(): string;
}
class ConcreteExample {
private identity: string = 'id';
doSomething() {
return 'done';
}
}
function passMeExample(input: Example) {
}
// OK!
passMeExample(new ConcreteExample());
So it is important to bear in mind that there is a difference in behaviour if you remove those private members. The classes will not act as they would if they were included.
A private method changes whether you can create a substitutable type, for example the declaration of Example
prevents a type match with our concrete class (whether or not we add a private identity member!)
declare class Example {
private identity: string;
public doSomething(): string;
}
class ConcreteExample {
private identity: string = 'id';
doSomething() {
return 'done';
}
}
function passMeExample(input: Example) {
}
// ConcreteExample is not allowable!
passMeExample(new ConcreteExample());
If you remove the private members, it changes the behaviour of the type checking:
declare class Example {
public doSomething(): string;
}
class ConcreteExample {
private identity: string = 'id';
doSomething() {
return 'done';
}
}
function passMeExample(input: Example) {
}
// OK!
passMeExample(new ConcreteExample());
So it is important to bear in mind that there is a difference in behaviour if you remove those private members. The classes will not act as they would if they were included.
answered Nov 20 '18 at 9:25


FentonFenton
152k42287311
152k42287311
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.
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%2f53389553%2fprivate-method-in-typescript-definitionsreact%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